I am writing a function to select values from data.table variables depending on conditions. The problem is that when the variable name does not match the name of the function argument, it is not selected correctly. The code is below.
library(data.table)
dt <- data.table(x = c(1, 2, 3, NA, NA),
y = c(2, 4, 3, 5, NA))
dd <- data.table(p = c(1, 2, 3, NA, NA),
q = c(2, 4, 3, 5, NA))
is.data.table(dt)
is.data.table(dd)
variable_chooser <- function(dt, x , y ) {
dt[!is.na(x), z := x]
dt[is.na(x) & !is.na(y), z := y]
dt[is.na (x) & is.na(y), z := NA]
}
variable_chooser(dt, dt$x, dt$y)
variable_chooser(dd, dd$p, dd$q)
dt
dd
The 2 data sets look like this at the end.
> dt
x y z
1: 1 2 1
2: 2 4 2
3: 3 3 3
4: NA 5 5
5: NA NA NA
> dd
p q z
1: 1 2 1
2: 2 4 2
3: 3 3 3
4: NA 5 2
5: NA NA NA
The dd
dataset has the value of the fourth row of the z
variable taken from the first row of q
rather than the fourth. With dt
, the code works as I expected.
How do I make the code for dd
work in the same way?
Thank you.