I need to fetch teacher name by matching the subject and board from studentdata and tutordata. Code--
Studentdata$Tutor.name <- Tutordata[cbind(
match(Studentdata$Subject, Tutordata$TSubject),
match(Studentdata$Board, colnames(Tutordata))
)]
Tutordata
Tname TSubject TBoard
A Physics IB, IGCSE
B Physics CBSE,JEE mains
C Math JEE mains
D Math IGCSE
E Physics ICSE
StudentData
StudentName Board Subject
X IB Math
Arjun IB Physics
Rehana IGCSE Physics
Rashid CBSE Math
Ashika JEE mainsMath
Aagya ICSE Math
All the variables are factors in the above dataset.
Probably, you can try to split TBoard
column into separate rows and then do a join.
library(dplyr)
tidyr::separate_rows(Tutordata, TBoard, sep = ",") %>%
mutate(TBoard = trimws(TBoard)) %>%
right_join(StudentData, by = c('TBoard' = 'Board', 'TSubject' = 'Subject'))
However, there doesn't seem any matches in your data.
sorry about the vague information. I have edited the question now.Problem is since the tutor board is a factor, i dont know now to parse through the data. I have attached the image of the output and input below. Sorry ,new to R programming
no. Not matching even when there is a match. Student rehana gets NA for tutor although there is a match. how to parse through a factor?
@KikuSekar Can you try the updated code?
Its working fine. Thanks a lot