I have two data frames with the same 3 columns: WeekNum, Year and Number.
A <- data.frame(WeekNum=c(1,2,3,4,5,1,2,3,4,5),
Year=c(2000,2000,2000,2000,2000,2001,2001,2001,2001,2001),
Number=c(0,0,0,0,0,0,0,0,0,0))
B <- data.frame(WeekNum=c(1,2,3,4,1,2,6),
Year=c(2000,2000,2000,2000,2001,2001,2001),
Number=c(0,1,0,1,2,5,6))
I want to create a new data frame with the same 3 columns using all WeekNum and Year combinations from A (and only those from B that are also present in A). When a WeekNum and Year combination is also present in B, I want to use the Number value from B. If the combination is not present in B, I want to leave the Number value as 0. Ultimately, I should have a data frame that looks like:
> C
WeekNum Year Number
1 1 2000 0
2 2 2000 1
3 3 2000 0
4 4 2000 1
5 5 2000 0
6 1 2001 2
7 2 2001 5
8 3 2001 0
9 4 2001 0
10 5 2001 0
A easy way is to create an
idcolumn for both data.framesAandBand then usematch:output: (
Edit:I see you changed your inputdata.frame)