tcl
tcl

Reputation: 19

How can I test a number of columns in a data.table for a specific value using R

This is my sample data

test <- data.table(name  = c("mike", "kate", "jeff"), ownership = c("1", "3", "4"), worth = c("1", "2", "3"), tax = c("4", "1", "4"))  

I am trying to test if any rows (people) contain a row that contains "1".

I can do it like this

test <- test[ownership == "1" | worth == "1" | tax == "1", status := "yes"]

but I would like to make it a bit more concise. Something like:

test <- test[any(c("ownership", "worth", "tax") %in% "1"), status := "yes"]

Upvotes: 2

Views: 200

Answers (2)

akrun
akrun

Reputation: 887213

We can use Reduce with .SD (data.table methods)

cols <- c('ownership', 'worth', 'tax')
i1 <- test[, Reduce(`+`, lapply(.SD, `==`, 1)) > 0, .SDcols = cols]
test[i1, yes := 'yes']
test
#   name ownership worth tax  yes
#1: mike         1     1   4  yes
#2: kate         3     2   1  yes
#3: jeff         4     3   4 <NA>

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388992

You can use rowSums :

cols <- c('ownership', 'worth', 'tax')
setDT(test)[rowSums(test[, ..cols] == 1) > 0, yes := 'yes']
test

#   name ownership worth tax  yes
#1: mike         1     1   4  yes
#2: kate         3     2   1  yes
#3: jeff         4     3   4 <NA>

Another option suggested by @chinsoon12

setDT(test)[test[, .I[rowSums(.SD==1L)], .SDcols=cols], status := "yes"] 

Upvotes: 4

Related Questions