快速过滤以下数据框架(原版很大)有任何好主意:对于具有相同值'成员'的行,只保留具有最小对应高度的行:#39;
group member height
A m 1
B m 2
C g 3
结果将是:
group member height
A m 1
C g 3
答案 0 :(得分:0)
你可以split
data.frame
,然后只返回与最小值相对应的行:
d <- data.frame(group = factor(LETTERS[1:3]), member = factor(rep(c("m", "g"), 2:1)),
height = 1:3)
do.call(rbind, lapply(split(d, d$member), function(.) .[which.min(.$height),]))
# group member height
# g C g 3
# m A m 1
我留给你删除不需要的rownames并按照你想要的顺序放置它。