问题
This is what my data.table looks like. The rightmost column PASTE
is my desired column.
library(data.table)
dt <- fread('
A B C PASTE
TRUE FALSE TRUE A,C
TRUE TRUE TRUE A;B;C
FALSE TRUE FALSE B
FALSE FALSE FALSE
')
I am trying to create the column PASTE
by concatenating all the column names as long as the value in that row for that column is TRUE.
This is my attempt:
dt[,PASTE:= if(dt[,c(1:3),with=FALSE] == TRUE, paste(names(dt), sep= ";"),"")]
回答1:
We can grouo by the rows, unlist
the Subset of Data.table (.SD
), subset the names of the dataset, paste
the elements together and assign (:=
) to 'newCol'
nm1 <- names(dt)[-4]
dt[, newCol := toString(nm1[unlist(.SD)]) ,by = 1:nrow(dt),.SDcols = nm1]
Or another option is melt
to 'long' format and then do a join
dt[melt(dt[, n := seq_len(.N)], id.var = c("n", "PASTE"))[,
toString(variable[value]), n], on = "n"]
来源:https://stackoverflow.com/questions/42209684/concatenate-column-names-in-data-table-based-on-conditions