Creating new column based on multiple possible cell possibilities across several columns

人盡茶涼 提交于 2021-02-08 08:25:50

问题


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

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.

Thoughts? Thanks!

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

回答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

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



来源:https://stackoverflow.com/questions/61047460/creating-new-column-based-on-multiple-possible-cell-possibilities-across-several

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