mercredi 24 juillet 2019

ifelse statement with string

double="true"

data=data.frame("var1"=c(1:10))

data$var2=ifelse(double=="true",2*data$var1,NA)
data$var2want=2*data$var1

I have a character that stores into double as "true" if I want to double a variable. In this example I start with var1 equal to 1:10. double="true". So I want to make var2 equals to (1:10)*2. The desired output is var2want. However, when I apply my ifelse statement I just get var2=2 for all values. I am not sure how to fix this issue.

Aucun commentaire:

Enregistrer un commentaire