I have 2 datasets, one contains school/subject quarter rate with 6 rows
and one contains subject national quarter rate with 8 rows. I want to combine
national quarter rate to the school subject rate, regardless of the existing of
school subject rate in that quarter. After join or merge, the class dataset should be 8 rows and qtr_rate_school should be NA in 2020Q4 and 2022Q2 rows AND sch_id should be 10001? Thanks
class <- data.frame(
stringsAsFactors = FALSE,
sch_id = c("10001", "10001", "10001", "10001", "10001", "10001"),
subj = c("40610", "40610", "40610", "40610", "40610", "40610"),
YrQtr = c("2020Q3","2021Q1","2021Q2",
"2021Q3","2021Q4","2022Q1"),
qtr_rate_school = c(224, 252.333333333333, 188, 144, NA, 295.666666666667)
)
nat <- data.frame(
stringsAsFactors = FALSE,
subj = c("40610","40610","40610",
"40610","40610","40610","40610","40610"),
YrQtr = c("2020Q3","2020Q4","2021Q1",
"2021Q2","2021Q3","2021Q4","2022Q1","2022Q2"),
qtr_rate_nat = c(152.132977487291,
198.50825617284,110.760384271671,129.086600051908,140.185767587582,
123.179853793799,159.337869537275,116.569458074953)
)
all <- full_join(class,nat,by=c('subj','YrQtr'))