I am trying to figure out how to conditionally replace values ββin a dataframe without using a loop. My data frame is structured as follows:
> df ab est 1 11.77000 2 0 2 10.90000 3 0 3 10.32000 2 0 4 10.96000 0 0 5 9.90600 0 0 6 10.70000 0 0 7 11.43000 1 0 8 11.41000 2 0 9 10.48512 4 0 10 11.19000 0 0
and dput output:
structure(list(a = c(11.77, 10.9, 10.32, 10.96, 9.906, 10.7, 11.43, 11.41, 10.48512, 11.19), b = c(2, 3, 2, 0, 0, 0, 1, 2, 4, 0), est = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("a", "b", "est"), row.names = c(NA, -10L), class = "data.frame")
What I want to do is check the value of b . If b is 0, I want to set the est value from a . I understand that df$est[df$b == 0] <- 23 set all est values ββto 23 when b==0 . I do not understand how to set est to a when this condition is true. For example:
df$est[df$b == 0] <- (df$a - 5)/2.533
gives the following warning:
Warning message: In df$est[df$b == 0] <- (df$a - 5)/2.533 : number of items to replace is not a multiple of replacement length
Is there a way I can pass the appropriate cell, not a vector?