I have two dataframes, A and B. The first dataframe contains years, groupnames, and names. The second dataframe records the complete group lists. I want to create a variable in A dataframe. If a name in A dataframe appear in the B's year/group list, it should be coded "Y", otherwise "N".
A dataframe and B dataframe are:
A <- data.frame(year = c("2000", "2000", "2000", "2000", "2002", "2002", "2003", "2003", "2003"), group = c("Star", "Star", "Sun", "Sun", "Mars", "Earth", "Earth", "Star", "Star"), name = c("John", "Bill", "Summer", "Evans", "Ben", "Mary", "Kally", "John", "Carl"))
B <- data.frame(year = c("2000", "2000", "2000", "2000", "2000", "2002", "2002","2002", "2003", "2003", "2003", "2003", "2003"), group = c("Star", "Star", "Star", "Sun", "Sun", "Mars", "Mars","Earth", "Earth", "Star", "Star", "Star", "Star"), namelist = c("John", "Helen", "Gray", "Summer", "Evans", "Kevin", "Ben", "Ring", "Steve", "Billy", "Carl", "Michel", "John"))
For example, in 2000, B dataframe shows that Star has John, Helen, and Gray. Therefore, because A dataframe's Star in 2000 has John and Carl, A dataframe's new variable's first two rows are "Y" and "N". The result should be like this:
year group name in_the_list
1 2000 Star John Y
2 2000 Star Bill N
3 2000 Sun Summer Y
4 2000 Sun Evans Y
5 2002 Mars Ben Y
6 2002 Earth Mary N
7 2003 Earth Kally N
8 2003 Star John Y
9 2003 Star Carl Y
B$in_the_list <- "Y"___new <- merge(A,B,by.x=c("year","group","name"),by.y=c("year","group","namelist"),all.x=TRUE)___new$in_the_list <- ifelse(is.na(new$in_the_list),"N","Y")- winampman