Home > Net >  Make a new column based on conditions, data.table R
Make a new column based on conditions, data.table R

Time:11-22

I have a function that contains a data.table with specific values:

 add_q <- function () {

DT = data.table (X = c("A", "A1", "B", "B1"), Y = c("C", "C1", "D", "D1"))}

This will give my two columns X and Y with the values. I want to make a 3rd column that would have the values YES for values that have A, C, A1, C1 and the value NO for the rest.

How to do that?

CodePudding user response:

Updated shortened code (many thanks to @B. Christian Kamgang:

Using fifelse we could do something like this:

library(data.table)

add_q <- function (DT, Z) {

  setDT(DT)[, Z :=  fifelse(grepl("A|C", X) | grepl("A|C", Y), "Yes", "NO")]
  DT
  }

DT = data.table (X = c("A", "A1", "B", "B1"), Y = c("C", "C1", "D", "D1"))

add_q(DT, Z)
DT

    X  Y   Z
1:  A  C YES
2: A1 C1 YES
3:  B  D  NO
4: B1 D1  NO

CodePudding user response:

We can try grepl like below

DT[
  ,
  Z := fifelse(
    Reduce(`&`, lapply(.SD, grepl, pattern = "^(A|C)")), 
    "YES", 
    "NO"
  )
]

which gives

> DT
    X  Y   Z
1:  A  C YES
2: A1 C1 YES
3:  B  D  NO
4: B1 D1  NO
  • Related