如何找到R数据框中的数字列数?
我们知道数据框可以包含任何类型的列,例如数字,字符,逻辑,因子等。并且,如果数据框包含多种类型的列,则我们可能希望查找每种类型或一个类型的列数输入数字。为此,我们可以将dplyr包的select_if函数与length函数一起使用,如以下示例所示。
例1
考虑以下数据帧-
> x1<-letters[1:20]输出结果> x2<-rnorm(20)
> x3<-rnorm(20)
> x4<-rpois(20,5)
> df1<-data.frame(x1,x2,x3,x4)
> df1
x1 x2 x3 x41 a -0.18404831 0.1082741 2
2 b -0.28597330 0.2584625 0
3 c 1.29158108 -0.5444644 6
4 d -0.80355312 -0.2261304 3
5 e -0.86895219 -0.9499907 4
6 f -0.69489165 0.4523057 3
7 g 0.70987445 -0.1152756 6
8 h 1.46023245 -1.5871850 8
9 i -0.14756283 0.4843958 4
10 j -1.46142329 0.7888207 12
11 k -0.20521299 -0.6228141 9
12 l 0.76613077 -1.3652169 9
13 m 0.07624931 0.3870339 5
14 n 1.22399304 0.4028503 3
15 o -0.09727281 -0.5263696 8
16 p -1.97470094 -0.1248541 4
17 q 0.11621840 -1.9126845 4
18 r -1.13008040 -1.5671634 1
19 s 0.73929690 -0.2571851 3
20 t -0.13389093 0.4876529 4
加载dplyr软件包并找到df1中的数字列数-
> library("dplyr")> length(select_if(df1,is.numeric))
[1] 3
例2
> y1<-LETTERS[1:20]输出结果> y2<-rpois(20,2)
> y3<-rpois(20,2)
> y4<-rpois(20,10)
> y5<-sample(c("Hot","Cold"),20,replace=TRUE)
> df2<-data.frame(y1,y2,y3,y4,y5)
> df2
y1 y2 y3 y4 y51 A 1 1 10 Cold
2 B 2 0 24 Hot
3 C 3 0 13 Cold
4 D 1 3 14 Cold
5 E 4 4 16 Hot
6 F 3 3 9 Cold
7 G 2 2 12 Hot
8 H 3 1 6 Hot
9 I 4 0 11 Hot
10 J 2 1 14 Hot
11 K 2 0 14 Hot
12 L 1 1 8 Hot
13 M 3 2 10 Hot
14 N 0 5 9 Cold
15 O 1 2 14 Hot
16 P 1 0 13 Cold
17 Q 7 2 13 Hot
18 R 1 2 4 Cold
19 S 1 1 7 Cold
20 T 3 3 10 Hot
在df中找到数字列的数量-
> length(select_if(df2,is.numeric))[1] 3
范例3
> z1<-sample(c("Male","Female"),20,replace=TRUE)输出结果> z2<-rpois(20,25)
> z3<-rnorm(20,2,0.3)
> df3<-data.frame(z1,z2,z3)
> df3
z1 z2 z31 Female 20 2.037433
2 Female 36 2.252606
3 Female 28 2.126866
4 Male 30 1.966581
5 Male 20 1.871318
6 Female 26 2.250764
7 Male 29 1.882002
8 Female 21 1.796225
9 Male 21 2.404416
10 Male 25 1.571489
11 Female 18 2.419949
12 Male 24 1.974340
13 Male 34 1.877119
14 Male 30 2.390536
15 Female 21 1.688357
16 Female 25 1.664844
17 Male 22 2.060667
18 Male 26 1.391200
19 Female 23 1.757949
20 Female 24 1.727739
在df3中找到数字列的数量-
> length(select_if(df3,is.numeric))[1] 2
以上是 如何找到R数据框中的数字列数? 的全部内容, 来源链接: utcz.com/z/319775.html