Assume the following table:
data <- data.table(dummy=1:10)
I know that you can do the following things:
data[dummy < 5, c("test1", "test2") := list("Yes", 1)]
and:
data[, test1 := fcase(dummy < 5, "Yes")]
data[, test2 := fcase(dummy < 5, 1)]
I am trying to combine these into one like so:
data[, c("test1", "test2") := fcase(dummy < 5, list("Yes", 1))]
But it gives me the following error:
Error in fcase(dummy < 5, list("Yes", 1)) :
Length of output value #2 must either be 1 or length of logical condition.
I need to go through multiple filters so it makes sense to use fcase. I can always resort to using the first solution for each filter like so:
data[dummy < 5, c("test1", "test2") := list("Yes", 1)]
data[dummy > 7, c("test1", "test2") := list("No", 0)]
data[between(dummy, 5, 7), c("test1", "test2") := list("Maybe", NA)]
but I am wondering if there isn't something more possible. There's also the solution of creating a table with each combination of test1 and test2 and merge this table with the data table after doing an fcase for only test1 like so:
tests <- data.table(test1 = c("Yes", "No", "Maybe"),
test2 = c(1, 0, NA))
data[, test1 := fcase(dummy < 5, "Yes",
dummy > 7, "No",
between(dummy, 5, 7), NA_character_)]
merge(data, tests, by = "test1", all.x = T, sort = F)
But this seems inefficient for a large and complex datatable