I am new to R and am stuck on what seems like an easy task - create new column vector in R data frame conditional on an existing character vector.
As an example, I have a data frame, "class", with one character column ("Names") and one numeric column ("Student_numbers"):
Names <- c("Sarah", "Mary", "Ben", "Will", "Alex")
Student_numbers <- c(3,5,6,7,7)
class <- data.frame(Names, Student_numbers)
To the data frame "class", I would like to add a new character column called "Gender" which is based on values in the character vector, "Names":
Male <- c("Ben", "Will", "Alex")
Female <- c("Sarah", "Mary")
Names Student_numbers Gender
1 Sarah 3 Female
2 Mary 5 Female
3 Ben 6 Male
4 Will 7 Male
5 Alex 7 Male
Instead of doing this manually, I would like to do it automatically based on the character vectors defined above.
Thank you in advance for your help.