R: group-wise min or max

2020-02-15 02:09发布

There are so many posts on how to get the group-wise min or max with SQL. But how do you do it in R?

Let's say, you have got the following data frame

ID | t | value
a | 1 | 3
a | 2 | 5
a | 3 | 2
a | 4 | 1
a | 5 | 5
b | 2 | 2
b | 3 | 1
b | 4 | 5

For every ID, I don't want the min t, but the value at the min t.

ID | value
a | 3
b| 2

3条回答
够拽才男人
2楼-- · 2020-02-15 02:21

Two more solutions (with sgibb's df):

sapply(split(df, df$ID), function(x) x$value[which.min(x$t)])

#a  b  
#3  2 

library(plyr)
ddply(df, .(ID), function(x) x$value[which.min(x$t)])

#  ID V1
#1 a   3
#2 b   2
查看更多
爷、活的狠高调
3楼-- · 2020-02-15 02:26

df is your data.frame -

library(data.table)

setDT(df) # convert to data.table in place

df[, value[which.min(t)], by = ID]

Output -

> df[, value[which.min(t)], by = ID]
   ID V1
1:  a  3
2:  b  2
查看更多
闹够了就滚
4楼-- · 2020-02-15 02:38

You are looking for tapply:

df <- read.table(textConnection("
ID | t | value
a | 1 | 3
a | 2 | 5
a | 3 | 2
a | 4 | 1
a | 5 | 5
b | 2 | 2
b | 3 | 1
b | 4 | 5"), header=TRUE, sep="|")

m <- tapply(1:nrow(df), df$ID, function(i) {
  df$value[i[which.min(df$t[i])]]
})
# a  b
#  3  2
查看更多
登录 后发表回答