【发布时间】:2017-03-13 05:15:24
【问题描述】:
我有一个包含重复商店/产品组合的数据框。我想删除重复的值,但我想保留这些产品每年的成本。
示例数据框:
store product year1 year2 year3
H&M shirt 20.00 29.95 NA
Mango trousers 49.95 NA NA
H&M trousers 39.95 NA 39.95
Mango trousers NA NA 44.95
我希望数据集看起来如何:
store product year1 year2 year3
H&M shirt 20.00 29.95 NA
H&M trousers 39.95 NA 39.95
Mango trousers 49.95 NA 44.95
我使用过 dplyr,但这似乎只是删除了重复项,而不是保留所有成本值。任何帮助表示赞赏!
可重现的代码:
df <- data.frame(store= c("H&M", "Mango", "H&M", "Mango"), product=c("shirt", "trousers", "trousers", "trousers"),
year1=c(20.95, 49.95, 39.95, NA), year2=c(29.95, NA, NA, NA), year3=c(NA,NA,39.95, 44.95))
【问题讨论】:
标签: r duplicates