I have this dataframe:
gender <- c("m", "m", "f", "f")
value <- c(75, 50, 20, 80)
df <- data.frame(gender, value)
df
I want to change the values in the column "value", conditional uppon the value on column "gender".
If the target change is a fixed number or character, the mutation is straightforward:
df$value[df$gender == "m"] <- 1
However, if the target value is a mutation of the original value, like this
df$value[df$gender == "m"] <- df$value * -1
I get the following error:
> Warning message:
> In df$value[df$gender == "m"] <- df$value * -1 :
> number of items to replace is not a multiple of replacement length
Is the conditional mutation with a variable target value possible in base R?