mercredi 4 décembre 2019

paste0 in ifelse R

I try to use paste0 in an ifelse() to create a conditioned new variable in a data table. The code I use is the following:

data[, paste0("inc_2014_",i) := ifelse(paste0("testvar",i) == 2014, 1, 0)]

This does not work. There is no error message but the values of inc_2014_i (in my test case inc_2014_1) are all 0 whereas ther should be a couple of hundreds 1.

If I test

data[, paste0("inc_2014_",i) := ifelse(testvar1 == 2014, 1, 0)]

it works fine.

If I test

paste0("testvar",i)

I get the correct variable as output.

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire