User981636
User981636

Reputation: 3621

R Data.Table create a variable with a condition

I need to create a new variable in the dataset below:

A  X
a  1
b  2
c  3
d  4
e  5
f  6
g  7
h  8
i  9
j 10

The newvar will have value 1 if X equals 2,5,7 or 9. Otherwise, newvar should be 0.

Code:

dt1 <- data.table(A = letters[1:10], X = 1:10, key = "X")
numberlist <- list(2,5,7,9)

I have tried the following based on a post here:

dt1[, newvar:=.SD, .SDcols = 0][%in% numberlist, newvar:=.SD, .SDcols = 1]
dt1[, newvar:=.SD, .SDcols = 0][X %in% numberlist, newvar:=.SD, .SDcols = 1]

dt1[, newvar:=.SD, .SDcols = 0] means "assign value of 0 to newvar as default option. The second bracket [%in% numberlist, newvar:=.SD, .SDcols = 1] means "if the key (X) is included in the numberlist, set the newvar value to 1.

Any idea why it is not working?

Upvotes: 3

Views: 2769

Answers (1)

akrun
akrun

Reputation: 886938

Try

dt1[, newvar:=(X %in% c(2,5,7,9))+0L][]
#     A  X newvar
# 1: a  1      0
# 2: b  2      1
# 3: c  3      0
# 4: d  4      0
# 5: e  5      1
# 6: f  6      0
# 7: g  7      1
# 8: h  8      0
# 9: i  9      1
#10: j 10      0

Or if we already have the matching elements stored in a a vector

numberlist <- c(2,5,7,9)
dt1[, newvar:=as.numeric(X %in% numberlist)] 

as.numeric is another option to coerce the logical vector to 0/1 values.

Upvotes: 3

Related Questions