dataframe - How to loop through columns in data.table in R -
this problem bothered me whole weekend. want count number of '1's in column based group of column. want loop through many columns, , example giving here simple case. think dt1 , dt2 should give me same result, dt1 doesn't work. can tell me reason 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), = sample.name] dt2 <- dt[, sum(class1 == 1), = sample.name]
starting character vector of column names, can use get
have column value:
round.test <- colnames(dt) round.test <- round.test[c(2, 3)] dt[, sum(get(round.test[1]) == 1), .(sample.name)] # sample.name v1 #1: 2 #2: b 0 #3: c 1 dt[, lapply(round.test, function(col) sum(get(col) == 1)), .(sample.name)] # sample.name v1 v2 #1: 2 2 #2: b 0 2 #3: c 1 1
or can use .sdcols
pass in column names, , access columns via .sd
:
dt[, lapply(.sd, function(col) sum(col == 1)), by=.(sample.name), .sdcols=round.test] # sample.name class1 class2 #1: 2 2 #2: b 0 2 #3: c 1 1
Comments
Post a Comment