【发布时间】:2015-01-19 07:39:07
【问题描述】:
我有一个如下的data.table
DT <- structure(list(V1 = structure(1:3, .Label = c("S01", "S02", "S03"), class = "factor"), V2 = structure(c(1L, 3L, 2L), .Label = c("Alan", "Bruce", "Jay"), class = "factor"), V3 = structure(c(3L, 1L, 2L), .Label = c("Barry", "Dick", "Hal"), class = "factor"), V4 = structure(c(1L, 3L, 2L), .Label = c("Guy", "Jean-Paul", "Wally"), class = "factor"), V5 = structure(c(3L, 1L, 2L), .Label = c("Bart", "Damien", "John"), class = "factor")), .Names = c("V1", "V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA, -3L))
setDT(DT)
setkey(DT, V1)
我正在尝试将 DT 中的选定列 (selcol) 粘贴到一个新列中。
selcol = c("V3", "V4")
我知道DT[, "NEW" := paste0(V3, V4), with = FALSE] 可以解决问题。但我想在代码中使用selcol。
我已经尝试了以下
DT[, "NEW" := paste0(which(colnames(DT) %in% selcol)), with = TRUE]
DT[, "NEW" := paste0(which(colnames(DT) %in% selcol)), with = TRUE]
DT[, "NEW" := paste0(3, 4), with = TRUE]
DT[, "NEW" := paste(3, 4), with = TRUE]
如何使用data.table 做到这一点。
【问题讨论】:
-
也许您对以下问题+答案也很感兴趣:stackoverflow.com/questions/26844251/…
标签: r data.table