0

In my dataset of 3 columns, 2 are strings, 1 is integer:

a<-read.table(text="X X.1 X.2
          A Z1  5
          B Z1  1
          C Z1  21
          E Z1  8
          F Z1  7
          G Z1  4
          H Z1  13
          I Z1  4
          J Z1  5
          A Z2  8
          B Z2  1
          D Z2  5
          E Z2  5
          F Z2  7
          G Z2  4
          H Z2  16
          I Z2  7
          J Z2  9
          A Z3  8
          B Z3  23
          C Z3  11
          D Z3  14
          E Z3  15
          ", header=TRUE)

I want to find the maximum and second largest of the dataset by groups; this means that I want to identify the maximum and second largest value of X in X.1 by its frequency (X.2). The code for identifying maximum by group that works for me is:

a.agg <- aggregate(X.2 ~ X.1, a, max)
(df.max <- merge(a.agg, a))
#   X.1 X.2 X
# 1  Z1  21 C
# 2  Z2  16 H
# 3  Z3  23 B

Now, the next step is to find the second largest occurring value of variable X by groups (X, X.1) as per the frequency variable X.2. The result should be 3 rows (because X.1 has 3 values) returning X by X.1 and by second biggest frequency identified from column X.2.

4

2 回答 2

2

我们能试试

library(data.table)
setDT(a)[order(-X.2),.SD[2], X.1]
#   X.1 X X.2
#1:  Z3 E  15
#2:  Z1 H  13
#3:  Z2 J   9
于 2015-12-14T07:49:04.370 回答
1

您需要做的就是用max返回第二大值的函数替换:

a.agg <- aggregate(X.2 ~ X.1, a, function(x) sort(x, decreasing=T)[2])
(df.second <- merge(a.agg, a))
#   X.1 X.2 X
# 1  Z1  13 H
# 2  Z2   9 J
# 3  Z3  15 E
于 2015-12-14T08:13:53.030 回答