简体   繁体   English

如何在R中的data.table中的列之间循环

[英]How to loop through columns in data.table in R

This problem bothered me the whole weekend. 这个问题困扰着我整个周末。 I want to count the number of '1's in a column based the group of another column. 我想基于另一列的组来计算一列中的“ 1”的数量。 I want to loop through many columns, and the example I am giving here is just a simple case. 我想遍历许多列,这里给出的示例只是一个简单的例子。 I think DT1 and DT2 should give me the same result, but obviously DT1 doesn't work. 我认为DT1和DT2应该给我相同的结果,但显然DT1不起作用。 Can anyone tell me what is the reason for this? 谁能告诉我这是什么原因? Thank you! 谢谢!

DT <- data.table(Sample.name = c("A","B","C","A","A","B"),
                 Class1 = c(1, 0, 1, 0, 1, 0),
                 Class2 = c(1, 1, 1, 0, 1, 1))

round.test <- colnames(DT)
round.test <- round.test[c(2, 3)]
round.test <- noquote(round.test)


DT1 <- DT[, sum((round.test[1]) == 1),
                  by = Sample.name]

DT2 <- DT[, sum(Class1 == 1),
          by = Sample.name]

Starting with a character vector of column names, you can use get to have the column value: 从列名的字符向量开始,可以使用get列值:

round.test <- colnames(DT)
round.test <- round.test[c(2, 3)]

DT[, sum(get(round.test[1]) == 1), .(Sample.name)]

#   Sample.name V1
#1:           A  2
#2:           B  0
#3:           C  1

DT[, lapply(round.test, function(col) sum(get(col) == 1)), .(Sample.name)]

#   Sample.name V1 V2
#1:           A  2  2
#2:           B  0  2
#3:           C  1  1

Or you can use .SDcols to pass in the column names, and access the columns via .SD : 或者,您可以使用.SDcols传入列名称,并通过.SD访问列:

DT[, lapply(.SD, function(col) sum(col == 1)), by=.(Sample.name), .SDcols=round.test]

#   Sample.name Class1 Class2
#1:           A      2      2
#2:           B      0      2
#3:           C      1      1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM