I have the 2 datasets below:
stud<-c("MARK","DGHN","BHKJ")
mark<-c(4,6,7)
d1<-data.frame(stud,mark)
stud2<-c("MARK","DGHN","BHKJ","dfgd","fg")
mark2<-""
d2<-data.frame(stud2,mark2)
I want to pass to the mark2
of d2
the values of mark
of d1
for the students that exist in both stud
and stud2
. The rest of the names of the stud2
should remain as they are but their respective mark2
values should be NAs
.I tried:
d2$stud2 <- factor(d2$stud2, levels=levels(d1$stud))
d2$mark2 <- ifelse(d2$stud2 == d1$stud, d1$mark, NA)
but not all of the stud2
values appear. Is there another way without having to mess with the fact that they do not have the same count of factors?