我有一个子集数据,该数据有一个较大数据集的每个观察值的总数。如果我想基于较高的计数删除重复项,并且如果名称相同,则删除显示的代码更少,该如何处理?例如:
name = c("a", "a", "b", "b", "b", "c", "d", "e", "e", "e")
code = c(1,1,2,3,4,1,1,2,2,3)
n = c(1,10,2,3,5,4,8,100,90,40)
data = data.frame(name,code,n)
最终产品将带有以下内容:
name = c("a", "b", "c", "d", "e")
code = c(1,4,1,1,2)
n = c(10,5,4,8,100)
data2 = data.frame(name,code,n)
答案 0 :(得分:1)
如果您可以使用dplyr,这应该可以解决问题:
library(dplyr)
data %>%
group_by(name) %>%
filter(n == max(n)) %>%
ungroup()