Reputation: 6501
I have this data frame:
> df
c1 c2
1 1 b
2 2 a
3 3 a
4 4 a
5 3 a
6 2 b
7 6 a
8 4 b
9 8 b
10 7 a
So for i=3, the output should be the 3rd lowest values of "c1" for both levels of "c2"
c1 c2
3 a
4 b
Upvotes: 3
Views: 167
Reputation: 8753
You have different options, one can be tapply
> df<-read.table(text=" c1 c2
1 1 b
2 2 a
3 3 a
4 4 a
5 3 a
6 2 b
7 6 a
8 4 b
9 8 b
10 7 a")
> df
c1 c2
1 1 b
2 2 a
3 3 a
4 4 a
5 3 a
6 2 b
7 6 a
8 4 b
9 8 b
10 7 a
> tapply(df$c1, df$c2, function(x) sort(x)[3])
a b
3 4
Or, using plyr
package, you can:
> library(plyr)
> ddply(df, .(c2), summarise, c1=sort(c1)[3])
c2 c1
1 a 3
2 b 4
Upvotes: 3
Reputation: 15458
Using data.table
library(data.table)
dt<-data.table(df1)
dt[,sort(c1)[3],by=c2]
c2 V1
1: b 4
2: a 3
Upvotes: 1
Reputation: 193507
Essentially a variation on a theme:
aggregate(c1 ~ c2, df, function(x) sort(x)[3])
# c2 c1
# 1 a 3
# 2 b 4
Upvotes: 4