Here is some mock data corresponding to the real dataset I am using:
mock dataset
a <- c("a","b","c","d","e","f","g","h","i","j")
b <- 1:10
names <-c("Alex","Ale","Alexandra","Alexander","Ali","Amanda","Alix","Ajax","Aley","Ajay")
data <- data.frame(a,b,names)
create new variable gender
data <- data %>%
mutate(gender = NA)
I want to assign a "gender" value to the names variable in my dataset. I don't want to do this manually because I am dealing with 1000s of observations. I do however have these variables, which contain the "names" value corresponding to the right gender:
male <- c("Alex", "Ale", "Alexander")
female <- c("Alexandra", "Ali", "Amanda")
noanswer <- c("Alix", "Ajax", "Aley", "Ajay")
However I don't know how to use them to assign a "gender" value to correspond with specific "names" in my dataset.
Here is what I tried:
data$gender[data$names== male] <- "Male"
And:
data$gender[data$names== c("Alex", "Ale", "Alexander")] <- "Male"
This code does not assign "Male" to all of the values. I recieve a warning message:
"Warning message:
In data$names == c("Alex", "Ale", "Alexander") :
longer object length is not a multiple of shorter object length"
Does anyone know how I can assign values to my gender variable corresponding to the names variable?