mardi 20 septembre 2016

Assigning an expression to a variable in R

I have one expression that I'd like to assign to a variable, because I'll be using it many times throughout a script (mostly in ifelse statements). But right now it's only giving me NA values.

so, for example, I'd like to use something like this:

 x <- df$var1 > 0.5

 x$var2 <- ifelse(x, 1, 0)

that isn't working, and var2 is just a column of NA values. But it currently works if I use:

 x$var2 <- ifelse(df$var1 > 0.5, 1, 0)

I thought it might be due to the quotes generated when you assign the expression to a variable, but this doesn't work either:

 x$var2 <- ifelse(noquote(x), 1, 0)

How do you appropriately assign an expression to a variable?

Aucun commentaire:

Enregistrer un commentaire