如何找出 R 数据帧中每个组合的总行数?


如果 R 数据帧包含两个或多个因子列,则因子的数量肯定有很多种组合,显然,如果因子的数量很大,级别也很高,则这些因子的级别组合也很大。要找出每个组组合的总行数,我们可以使用变形函数。

范例

考虑下面的数据帧 −

 现场演示

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

输出

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

范例

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

输出

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

我们再来看一个具有相似数据的另一个示例 −

范例

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

输出

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

范例

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

输出

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

更新于:2020-08-24

207 次浏览

启动你的 职业

完成本课程即可获得认证

开始
广告