Concatenate column names in data.table based on conditions [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 02:11:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!