Home > Net >  Find gender based on a specific dataframe
Find gender based on a specific dataframe

Time:11-22

Having a dataframe with have the gender of specific names

dfgender <- data.frame(name = c("Helen","Erik"), gender = c("F","M"))

How is it possible to use the previous data frame in order to check the names of another column of a dataframe and insert "Neutral" if the name is not in the list of gender dataframe: Example of the dataframe with the names:

dfnames <- data.frame(names = c("Helen", "Von", "Erik", "Brook"))

Example of expected output

dfnames <- data.frame(name = c("Helen", "Von", "Erik", "Brook"), gender = c("F", "Neutral", "M", "Neutral"))

CodePudding user response:

left_join replace_na should do:

dfnames %>% left_join(dfgender, by=c('names' = 'name')) %>% 
  mutate(gender = gender %>% as.character %>% replace_na('Neutral'))

# names  gender
# 1 Helen       F
# 2   Von Neutral
# 3  Erik       M
# 4 Brook Neutral

CodePudding user response:

The (experimental) rows_update could be an intuitive compliment to @Juan C's answer:

library(dplyr)

dfnames |>
  mutate(gender = "Neutral") |>
  rows_update(rename(dfgender, names = name), "names")

Output:

  names  gender
1 Helen       F
2   Von Neutral
3  Erik       M
4 Brook Neutral
  •  Tags:  
  • r
  • Related