我有以下数据:
df <- data.frame(A = c(1,2,3,4,5,6), B=c("P","P","P","Q","Q","Q"), C=c("a","b","c","d","e","f"))
df
## A B C
## 1 1 P a
## 2 2 P b
## 3 3 P c
## 4 4 Q d
## 5 5 Q e
## 6 6 Q f
我想以某种方式为每个不同的B获取A中具有最小值的行,但也使用C中的相应值。例如
## A B C
## 1 1 P a
## 4 4 Q d
我尝试了以下方法,但我想要的也没有:
> aggregate(df[c('A')], by=df[c('B')], FUN=min)
B A
1 P 1
2 Q 4
> aggregate(df[c('A')], by=df[c('B','C')], FUN=min)
B C A
1 P a 1
2 P b 2
3 P c 3
4 Q d 4
5 Q e 5
6 Q f 6
答案 0 :(得分:5)
你可以尝试
library(dplyr)
df %>%
group_by(B) %>%
filter(A==min(A))
# A B C
#1 1 P a
#2 4 Q d
或者
library(data.table)
setDT(df)[, .SD[A==min(A)], B]
或使用base R
df[with(df, ave(A, B, FUN=min)==A),]
# A B C
#1 1 P a
#4 4 Q d
答案 1 :(得分:1)
你也可以使用split-apply技术:
# split `df` on the field 'b'
tmp <- split(df,df$B)
# reduce to the row with the minimum value of A
tmp <- lapply(tmp,function(x)
x[x$A == min(x$A),])
# bind the rows together
do.call(rbind,tmp)
#> A B C
#> P 1 P a
#> Q 4 Q d