How to find the total number of rows per group combination in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

An R data frame that contains two or more factor columns then there are a greater number of combinations for the number of factors, obviously, if the number of factors is large with large number of levels then the combination of levels of the factors is also large. To find total number of rows per group combination we can use transform function.

Example

Consider the below data frame −

 Live Demo

set.seed(101)
Group<-rep(c("G1","G2","G3","G4","G5"),times=4)
Class<-rep(c("A","B","C"),times=c(8,7,5))
Frequency<-sample(1:100,20)
df<-data.frame(Group,Class,Frequency)
df

Output

Group Class Frequency
1 G1 A 73
2 G2 A 57
3 G3 A 46
4 G4 A 95
5 G5 A 81
6 G1 A 58
7 G2 A 61
8 G3 A 60
9 G4 B 59
10 G5 B 3
11 G1 B 32
12 G2 B 9
13 G3 B 31
14 G4 B 93
15 G5 B 53
16 G1 C 92
17 G2 C 49
18 G3 C 14
19 G4 C 76
20 G5 C 45

Example

transform(df,Total=ave(Frequency,Group,Class, FUN = length))

Output

Group Class Frequency Total
1 G1 A 73 2
2 G2 A 57 2
3 G3 A 46 2
4 G4 A 95 1
5 G5 A 81 1
6 G1 A 58 2
7 G2 A 61 2
8 G3 A 60 2
9 G4 B 59 2
10 G5 B 3 2
11 G1 B 32 1
12 G2 B 9 1
13 G3 B 31 1
14 G4 B 93 2
15 G5 B 53 2
16 G1 C 92 1
17 G2 C 49 1
18 G3 C 14 1
19 G4 C 76 1
20 G5 C 45 1

Let’s have a look at another example with similar data −

Example

set.seed(101)
Group<-rep(c("G1","G2"),times=10)
Class<-rep(c("A","B","C"),times=c(8,7,5))
df2<-data.frame(Group,Class,Frequency)
set.seed(101)
Group<-rep(c("G1","G2"),times=10)
Class<-rep(c("A","B","C"),times=c(8,7,5))
Frequency<-sample(1:100,20)
df2<-data.frame(Group,Class,Frequency)
df2

Output

Group Class Frequency
1 G1 A 73
2 G2 A 57
3 G1 A 46
4 G2 A 95
5 G1 A 81
6 G2 A 58
7 G1 A 61
8 G2 A 60
9 G1 B 59
10 G2 B 3
11 G1 B 32
12 G2 B 9
13 G1 B 31
14 G2 B 93
15 G1 B 53
16 G2 C 92
17 G1 C 49
18 G2 C 14
19 G1 C 76
20 G2 C 45

Example

transform(df2,Total=ave(Frequency,Group,Class, FUN = length))

Output

Group Class Frequency Total
1 G1 A 73 4
2 G2 A 57 4
3 G1 A 46 4
4 G2 A 95 4
5 G1 A 81 4
6 G2 A 58 4
7 G1 A 61 4
8 G2 A 60 4
9 G1 B 59 4
10 G2 B 3 3
11 G1 B 32 4
12 G2 B 9 3
13 G1 B 31 4
14 G2 B 93 3
15 G1 B 53 4
16 G2 C 92 3
17 G1 C 49 2
18 G2 C 14 3
19 G1 C 76 2
20 G2 C 45 3
raja
Published on 24-Aug-2020 11:39:15
Advertisements