如何找到基于行值的R数据帧满足条件的列数?

有时我们想从数据帧中提取计数,该计数可能是基于行值具有相同特征的列数。例如,如果我们有一个包含三列和五十行的数据框,并且值是1到100之间的整数,那么我们可能想要查找每行的值大于20的列数。这可以通过使用rowSums函数来完成。

示例

请看以下数据帧-

> x1<-sample(1:10,20,replace=TRUE)
> x2<-sample(1:100,20)
> x3<-rpois(20,5)
> df<-data.frame(x1,x2,x3)
> df

输出结果

 x1 x2 x3
1 9 72 9
2 5 20 6
3 3 82 4
4 5 47 4
5 1 45 10
6 6 14 6
7 10 54 7
8 10 13 6
9 4 98 5
10 4 76 5
11 5 53 5
12 9 87 2
13 3 79 6
14 2 73 5
15 10 75 3
16 1 7 2
17 5 92 7
18 5 34 5
19 9 52 5
20 5 43 4

将新列添加到df中,其中数字列的值大于5-

示例

> df$Number_of_columns_LargerThan5<-rowSums(df>5)
> df

输出结果

 x1 x2 x3 Number_of_columns_LargerThan5
1 9 72 9    3
2 5 20 6    2
3 3 82 4    1
4 5 47 4    1
5 1 45 10   2
6 6 14 6    3
7 10 54 7   3
8 10 13 6   3
9 4 98 5    1
10 4 76 5   1
11 5 53 5   1
12 9 87 2   2
13 3 79 6   2
14 2 73 5   1
15 10 75 3  2
16 1 7 2    1
17 5 92 7   2
18 5 34 5   1
19 9 52 5   2
20 5 43 4   1

将数值小于5的数字列添加到df中的新列-

示例

> df$Number_of_columns_LessThan5<-rowSums(df<5)
> df

输出结果

x1 x2 x3 Number_of_columns_LargerThan5 Number_of_columns_LessThan5
1 9 72 9          3                         1
2 5 20 6          2                         1
3 3 82 4          1                         3
4 5 47 4          1                         2
5 1 45 10         2                         2
6 6 14 6          3                         1
7 10 54 7         3                         1
8 10 13 6         3                         1
9 4 98 5          1                         2
10 4 76 5         1                         2
11 5 53 5         1                         1
12 9 87 2         2                         2
13 3 79 6         2                         2
14 2 73 5         1                         2
15 10 75 3        2                         2
16 1 7 2          1                         3
17 5 92 7         2                         1
18 5 34 5         1                         1
19 9 52 5         2                         1
20 5 43 4         1                         2

让我们看另一个例子-

示例

> y1<-sample(1:100,20)
> y2<-sample(1:1000,20)
> df_y<-data.frame(y1,y2)
> df_y

输出结果

   y1 y2
1 33 663
2 20 523
3 24 791
4 100 330
5 48 264
6 32 579
7 56 51
8 94 57
9 76 711
10 58 411
11 49 849
12 63 805
13 67 696
14 1 237
15 11 147
16 12 448
17 75 465
18 65 220
19 99 958
20 34 909
> df_y$Number_of_columns_less_than_equalto_50<-rowSums(df_y<=50)
> df_y

输出结果

  y1 y2 Number_of_columns_less_than_equalto_50
1 33 663    1
2 20 523    1
3 24 791    1
4 100 330   0
5 48 264    1
6 32 579    1
7 56 51     0
8 94 57     0
9 76 711    0
10 58 411   0
11 49 849   1
12 63 805   0
13 67 696   0
14 1 237    1
15 11 147   1
16 12 448   1
17 75 465   0
18 65 220   0
19 99 958   0
20 34 909   1