基于跨多个列的多个可能的单元格可能性来创建新列 [英] Creating new column based on multiple possible cell possibilities across several columns

查看:34
本文介绍了基于跨多个列的多个可能的单元格可能性来创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data [,allkneePR:= Reduce(`|`,lapply(.SD,`==`,"0082")),.SDcols = PR1:PR3]

嘿,我试图在data.table(数据集很大)中的一系列行和列中寻找不同的诊断c("0082","0083","0084").如果任何一个PR1:PR3列中的值之一是"0082","0083"或"0084",则我希望另一列指示为true.现在,这适用于上面的代码,但是我试图添加多个诊断,而不仅仅是"0082".我尝试了不起作用的any()函数,仅使用向量c("0082","0083","0084")无效.

Hey, I'm trying to look for different diagnoses c("0082", "0083", "0084") across a range of rows and columns in data.table (the dataset is huge). If one of the values is "0082" or "0083" or "0084" in any of the columns PR1:PR3 I want another column that indicates true. Right now this works with the above code, but I am trying to add in multiple diagnoses, not just "0082". I tried the any() function which doesn't work, and just using a vector c("0082", "0083", "0084") doesn't work.

有什么想法吗?谢谢!

有趣的练习数据集在这里:

Fun practice dataset is here:

data <- as.data.table(data.frame(PR1 = c("0081", "0082", "0083", "0084", "8154"), PR2 = c("12","0084", "1","3", "9"), PR3 = c("9", "12", "25", "0083", "8154")))

data[, allkneePR := Reduce(`|`, lapply(.SD, `==`, "0082")), .SDcols=PR1:PR3]
data

推荐答案

我们可以使用%in%代替 == 来比较长度大于1的向量

We can use %in% instead of == for comparing a vector of length greaterr than 1

library(data.table)
data[, allkneePR := Reduce(`|`, lapply(.SD, `%in%`, 
               c("0082", "0083", "0084"))), .SDcols=PR1:PR3]

data
#    PR1  PR2  PR3 allkneePR
#1: 0081   12    9     FALSE
#2: 0082 0084   12      TRUE
#3: 0083    1   25      TRUE
#4: 0084    3 0083      TRUE
#5: 8154    9 8154     FALSE

如果列是字符,则可以将%in%更改为%chin%

If the columns are character, can change %in% to %chin%

这篇关于基于跨多个列的多个可能的单元格可能性来创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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