如何遍历数据表列? [英] How to loop through a datatable columns?

查看:64
本文介绍了如何遍历数据表列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将数据表的列转换为另一个类,并且我无法使用字符串引用列。

I am looking to convert the columns of a datatable into another class and I am stuck with the inability to refer to the columns using strings.

set.seed(10238)
idt <- data.table(A = rep(1:3, each = 5), B = rep(1:5, 3),
                 C = sample(15), D = sample(15)) 

> idt
    A B  C  D
 1: 1 1 10 14
 2: 1 2  2  2
 3: 1 3 13  3
 4: 1 4  7  1
 5: 1 5  1  8
 6: 2 1 11 15
 7: 2 2  4 10
 8: 2 3 15  7
 9: 2 4 14 12
10: 2 5  5  9
11: 3 1  8 13
12: 3 2  3  4
13: 3 3  9  6
14: 3 4  6 11
15: 3 5 12  5

#All columns are integers 
    > lapply(idt, class)
$A
[1] "integer"
$B
[1] "integer"
$C
[1] "integer"
$D
[1] "integer"

vec = parse(text=c('A','B','C','D'))
for (i in vec) idt[, eval( i ) := as.character( eval(i) ) ]
Error in eval(expr, envir, enclos) : object 'A' not found*

我想通过遍历包含表示字符串的向量的向量来重新分配列类我要转换的列的名称。

I want to reassign the columns classes by looping through a vector containing the strings representing the names of the columns I want to convert.

我知道其他线程解决了相同的问题,但不是很容易理解。我的问题是为什么我不能遍历表达式和 eval ,就像我会手动将j表达式中的i替换为每列的列名一样。

I am aware of other threads adressing the same problem but they are not very understandable. My question is why can't I loop through expressions and eval them just like I would do manually replacing the the i in the j-expressions with the column names for each column.

** 编辑非重复 **


我知道其他线程解决了相同的问题,但是它们不是很容易理解我的问题是为什么我不能遍历表达式和 eval ,就像我会手动将j表达式中的i替换为每列的列名一样。

I am aware of other threads adressing the same problem but they are not very understandable. My question is why can't I loop through expressions and eval them just like I would do manually replacing the the i in the j-expressions with the column names for each column.


推荐答案

我们可以使用 通过遍历 idt的列名来循环。在这种情况下,我们获取字符串的值,将其转换为字符并赋值( := )转换为字符串名称或列名称((i)

We can do this with a for loop by looping over the column names of 'idt'. In this case we get the values of the string, convert it to character and assign (:=) it to the string name or column name ((i))

vec <- names(idt)
for(i in vec) idt[, (i) := as.character(get(i))]






或使用 .SDcols ,我们在 .SDcols 中指定感兴趣的列,遍历data.table的子集( .SD )与 lapply 并分配(:= )到列名('vec')的向量


Or using .SDcols, we specify the columns of interest in .SDcols, loop through the Subset of data.table (.SD) with lapply and assign (:=) it to the vector of column names ('vec')

id1[, (vec) := lapply(.SD, as.character), .SDcols = vec]

这篇关于如何遍历数据表列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆