根据条件连接data.table中的列名 [英] Concatenate column names in data.table based on conditions

查看:55
本文介绍了根据条件连接data.table中的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的data.table的样子.最右边的列PASTE是我想要的列.

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   
    ')

我试图通过串联所有列名来创建列PASTE,只要该行在该列中的值为TRUE.

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.

这是我的尝试:

dt[,PASTE:= if(dt[,c(1:3),with=FALSE] == TRUE, paste(names(dt), sep= ";"),"")]

推荐答案

我们可以按行unlist Data.table的子集(.SD),数据集名称的子集paste元素并分配(:=)到'newCol'

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]


或者另一种选择是melt为'long'格式,然后进行连接


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"]

这篇关于根据条件连接data.table中的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆