I have a data frame (zz) with three variables (v1, v2 and v3). I want to mutate a new variable v4 based on some conditions.
- if v3 <= 1 then v4=(v1+v2)/2....the mean.
- if v3 > 1 then v4 = V1 or V2 whichever is greater.
- if either of v1 or v2 is NA then v4 is the value of the non-NA variable.
- if both v1 and v2 are NA then v4 is also NA The script below generates sample data and my start to the solution
v1 <- c(4, 4, 4, 4, 3, 5, 6, 7, 2, NA )
v2 <- c(6, 2, 3, 3, 4, 4, 5, 7, NA, 4)
v3 <- c(2, 2, 1, 1, 1, 2, 1, 0, NA, NA)
zz <- data.frame(v1, v2, v3)
zz = mutate(zz, v4 = ifelse( v3<=1, (v1+v2)/2,
ifelse( ?????)
Any guidance would be helpful
Aucun commentaire:
Enregistrer un commentaire