I have a data set that is being updated every year. Now I have a new data and I have to fill in some columns based on a specific column
I need to match whatever information I have in 'Gender' column with its respective columns, so the end results it should look like this
I tried using split() but it didn't... Thank you for all the help
You can use do this using mutate
from dplyr
package and ifelse
.
Here is a little example:
`library(dplyr)
df <- data.frame(Year = c(2018, 2019),
Gender = c(NA, "1,3"))
df %>% mutate(Gen_Fema = ifelse(is.na(Gender), NA ,
ifelse(grepl("1",Gender), "Yes", "No")),
Gen_Male = ifelse(is.na(Gender), NA ,
ifelse(grepl("2",Gender), "Yes", "No")),
Gen_Trans = ifelse(is.na(Gender), NA ,
ifelse(grepl("3",Gender), "Yes", "No")),
Gen_other = ifelse(is.na(Gender), NA ,
ifelse(grepl("4",Gender), "Yes", "No")))
Output:
Year Gender Gen_Fema Gen_Male Gen_Trans Gen_other
1 2018 <NA> <NA> <NA> <NA> <NA>
2 2019 1,3 Yes No Yes No