Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何通过数据框中某个变量的最大值创建子集?
我有以下数据框,我想从中按名称提取 V1 的最大值:
name V1 V2 1 name1 1 f 2 name1 2 b 3 name1 5 c 4 name2 3 f 5 name2 8 g 6 name2 2 m
子集应如下所示
name V1 V2 3 name1 5 c 5 name2 8 g
我们可以使用dplyr. 按“名称”分组后slice,具有“V1”最大值的行
dplyr
slice
library(dplyr) df1 %>% group_by(name) %>% slice(which.max(V1)) # name V1 V2 # <chr> <int> <chr> #1 name1 5 c #2 name2 8 g