R how to find maximum value by a primary key and removing duplicates -
this question has answer here:
i have dataset below , want reduce removing rows. in situation have more 1 attach value same exact combination of first 3 columns, want keep rows have maximum attach value
bom = c(rep("bom1", 1), rep("bom2", 2), rep("bom3", 3)) part = c(rep("a", 3), rep("d", 3)) ww = c(rep("ww01",3),rep("ww05",1),rep("ww06",2)) attach = c(1,4,8,2,2,4) df1 = data.frame(bom,part,ww,attach )
the final output have row numbers 1,3,4,6 , remaining rows deleted
you can dplyr:
library(dplyr) df1 %>% group_by(bom, part, ww) %>% summarise(attach = max(attach)) source: local data frame [4 x 4] groups: bom, part bom part ww attach 1 bom1 ww01 1 2 bom2 ww01 8 3 bom3 d ww05 2 4 bom3 d ww06 4
Comments
Post a Comment