I'm really new to R, so excuse my question, it might seem stupid. I want to create a new column in R using the dplyr function.
Gender Value
boy 2
girl 3
girl 2
This the table that I have. I want to create a new column that has as one condition the gender and then either the Value or a 0. Kind of like this:
gender Value Girl
boy 2 0
girl 3 3
girl 2 2
So far I have the function: df1 <- mutate(df, Girl= ifelse(Gender== "girl",Value,0))
But that way, it doesn't assign the Value to the column, I just get 0's. Do you know a way to fix this?
CodePudding user response:
It is possible that there are some leading/lagging spaces. Also, the code showed that the OP was creating the logical condition in Gender
. In the expected, the column name was lower case (R
is case sensitive)
library(dplyr)
df %>%
mutate(Girl = ifelse(trimws(gender) == "girl", Value, 0))