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.