I need to update every ID line item in my table with the max(COFlag). 2nd picture is desired output. I've tried: merge(x=DF1, y=DF2, by = 'ID'. I get the error 'by' must specify a uniquely valid column
Asked
Active
Viewed 32 times
0
-
2Welcome to Stack Overflow. Please don’t use images of data as they cannot be used without a lot of unnecessary effort. [For multiple reasons](//meta.stackoverflow.com/q/285551). You’re more likely to get a positive response if your question is reproducible. [See Stack Overflow question guidance](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Mar 15 '22 at 20:27
-
I don't think `merge` is the right concept, I suspect it's just [summarize by group](https://stackoverflow.com/q/11562656/3358272), but I think we're missing something. How do you determine that a particular value in `COFlag` should be `0` or `1`? Since you have four distinct `ID` fields, I don't think that that is the key for grouping, it appears that `MonthYear` is more appropriate here. Please explain more about how to determine 0s and 1s. – r2evans Mar 15 '22 at 20:37
-
DF2 <- Df1 %>% group_by(ID) %>% summarise(COFlag = max(COFlag, na.rm=TRUE)) – rhaug2 Mar 15 '22 at 20:43
1 Answers
-1
inner_join(df1,df2, by= 'ID')
gave me my desired output.

rhaug2
- 1
- 2
-
2I find it funny that this worked. This produces the same results as `merge(DF1, DF2, by="ID")` (in concept, though we don't know your data). – r2evans Mar 15 '22 at 21:55