R newbie here. I am currently trying to recode a set of variables in R, such that all negative values within them are recoded as positive values.
Please find simulated data below:
df <- data.frame(ID = c(1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011),
measure1 = rnorm(11, mean = 0, sd = 1),
measure2 = rnorm(11, mean = 0, sd = 1),
measure3 = rnorm(11, mean = 0, sd = 1),
measure4 = rnorm(11, mean = 0, sd = 1))
This is the function I've written so far:
df[2:5] <- sapply(df[2:5], function(x) {
if(x<0) {
return(x*-1)
} else {
return(x)
}
})
although I recieve the following errors:
Warning messages:
1: In if (x < 0) { :
the condition has length > 1 and only the first element will be used
2: In if (x < 0) { :
the condition has length > 1 and only the first element will be used
3: In if (x < 0) { :
the condition has length > 1 and only the first element will be used
4: In if (x < 0) { :
the condition has length > 1 and only the first element will be used
Any ideas on how to make this work?
Thanks!
Aucun commentaire:
Enregistrer un commentaire