2015-01-19 6 views
7

से मैं के रूप में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) चिपकाने के लिए कोशिश कर रहा हूँ एक data.table है।

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 के साथ ऐसा कैसे।

+1

शायद निम्नलिखित प्रश्न + जवाब भी आप के लिए दिलचस्प है: http://stackoverflow.com/questions/26844251/data-table-using-with-false-and-transforming-function-summary-function/ 26860531 # 26860531 – Julian

उत्तर

8
DT[, NEW := do.call(paste0, .SD), .SDcols = selcol] 
# V1 V2 V3  V4  V5   NEW 
#1: S01 Alan Hal  Guy John  HalGuy 
#2: S02 Jay Barry  Wally Bart BarryWally 
#3: S03 Bruce Dick Jean-Paul Damien DickJean-Paul 
संबंधित मुद्दे