如何使用 R 数据框中元素大小小于 3 的字符列删除行?
要查找字符向量元素中的字符数或R数据帧的字符列中的元素,我们可以使用nchar函数。因此,如果我们想删除元素大小小于3的行,我们需要使用相同的函数,然后将使用子集函数删除所需的行,如下例所示。
示例1
考虑以下数据框-
> x1<-sample(c("India","UK","China"),20,replace=TRUE) > x2<-rpois(20,2) > df1<-data.frame(x1,x2) > df1输出结果
x1 x2 1 India 1 2 India 2 3 UK 1 4 UK 2 5 China 1 6 India 2 7 UK 1 8 India 0 9 China 2 10 China 2 11 China 0 12 India 4 13 India 3 14 China 2 15 China 1 16 China 2 17 China 1 18 China 1 19 China 4 20 China 2
删除df1中字符大小小于3的行-
> subset(df1,nchar(as.character(df1$x1))>=3)输出结果
x1 x2 1 India 1 2 India 2 5 China 1 6 India 2 8 India 0 9 China 2 10 China 2 11 China 0 12 India 4 13 India 3 14 China 2 15 China 1 16 China 2 17 China 1 18 China 1 19 China 4 20 China 2
例2
> y1<-sample(c("Yes","No"),20,replace=TRUE) > y2<-rnorm(20) > df2<-data.frame(y1,y2) > df2输出结果
y1 y2 1 No -1.7364659 2 No 1.0939593 3 No -0.3927835 4 No -0.2663386 5 Yes 0.2212613 6 No -0.4846801 7 No 2.5305836 8 No -1.1580186 9 Yes 1.2991126 10 No -0.2289025 11 No -0.7304356 12 Yes 0.3648929 13 Yes -0.5454145 14 No 0.7025904 15 No -0.1482001 16 No 0.3592025 17 Yes 1.7478691 18 No -0.2124407 19 No 0.4227296 20 Yes -1.7340860
删除df2中字符大小小于3的行-
> subset(df2,nchar(as.character(df2$y1))>=3)输出结果
y1 y2 5 Yes 0.2212613 9 Yes 1.2991126 12 Yes 0.3648929 13 Yes -0.5454145 17 Yes 1.7478691 20 Yes -1.7340860
例3
> z1<-sample(c("Male","Female","NA"),20,replace=TRUE) > z2<-runif(20,1,10) > df3<-data.frame(z1,z2,z3) > df3输出结果
z1 z2 z3 1 Male 4.194956 2.037433 2 Male 5.223558 2.252606 3 Male 2.134314 2.126866 4 Female 8.728642 1.966581 5 Male 5.105030 1.871318 6 Female 8.249922 2.250764 7 NA 5.662960 1.882002 8 Male 6.712668 1.796225 9 NA 5.421763 2.404416 10 Female 5.588083 1.571489 11 NA 7.013066 2.419949 12 Female 2.863304 1.974340 13 Male 6.677141 1.877119 14 Female 6.357583 2.390536 15 Male 5.130621 1.688357 16 Male 2.709292 1.664844 17 Male 2.421428 2.060667 18 Female 2.900265 1.391200 19 NA 5.583389 1.757949 20 Female 3.149392 1.727739
删除df3中字符大小小于3的行-
> subset(df3,nchar(as.character(df3$z1))>=3)输出结果
z1 z2 z3 1 Male 4.194956 2.037433 2 Male 5.223558 2.252606 3 Male 2.134314 2.126866 4 Female 8.728642 1.966581 5 Male 5.105030 1.871318 6 Female 8.249922 2.250764 8 Male 6.712668 1.796225 10 Female 5.588083 1.571489 12 Female 2.863304 1.974340 13 Male 6.677141 1.877119 14 Female 6.357583 2.390536 15 Male 5.130621 1.688357 16 Male 2.709292 1.664844 17 Male 2.421428 2.060667 18 Female 2.900265 1.391200 20 Female 3.149392 1.727739