I have 2 datasets like this:
df1 <- data.frame(name=c(harry, ron, hermione),
surname = c("potter","weasley","granger")
)
df2 <- data.frame(surname = c("alton", "alton", "alves",
"weasley","weasley", "weasley, "potter",
"potter", "bell", "granger"),
house=c("Gryffindor","Ravenclaw", "Hufflepuff","Gryffindor", "Slytherin", "Gryffindor",
"Hufflepuff", "Gryffindor", "Slytherin", "Ravenclaw")
)
For each person in df1, I want to count the number of people with a common surname in df2 for each house separately. You can think of it as an attempt to trace each person's ancestors' history in Hogwarts by looking at the surnames in each House so far.
So, the outcome should be something like this:
df_new <- data.frame(surname = c("potter","weasley","granger"),
Hufflepuff= c(NUMBER OF COMMON SURNAME,NUMBER OF COMMON SURNAME,NUMBER OF COMMON SURNAME),
Gryffindor= c(NUMBER OF COMMON SURNAME,NUMBER OF COMMON SURNAME,NUMBER OF COMMON SURNAME),
Slytherin= c(NUMBER OF COMMON SURNAME,NUMBER OF COMMON SURNAME,NUMBER OF COMMON SURNAME),
Ravenclaw= c(NUMBER OF COMMON SURNAME,NUMBER OF COMMON SURNAME,NUMBER OF COMMON SURNAME)
)
Thank you!