使用group_by()
我想基于列value
的最大值来获取列value2
的值:
df = data.frame(id = c(1,1,1,1,2,2,2,2),
value = c(4,5,1,3,1,2,3,1),
value2 = c("a","b","c","d","e","f","g","h"))
df %>% group_by(id) %>%
sumarise(value2_of_largest_value = f(value, value2))
1 b
2 g
答案 0 :(得分:4)
我们可以使用which.max
来获取value
的索引,并使用它来对value2
进行子集化
library(dplyr)
f1 <- function(x, y) y[which.max(x)]
df %>%
group_by(id) %>%
summarise(value2 = f1(value, value2))
#or simply
# summarise(value2 = value2[which.max(value)])
# A tibble: 2 x 2
# id value2
# <dbl> <fct>
#1 1 b
#2 2 g
答案 1 :(得分:1)
library(dplyr)
df1 %>%
group_by(id) %>%
filter(value == max(value))
中的另一种方法:
data.table
或在library(data.table)
setDT(df1)[setDT(df1)[, .I[value == max(value)], by=id]$V1]
中:
cl_hard_wired_encryptor