如何在R数据框中检查列是否存在?
如果我们有非常大的数据集,那么我们很可能会忘记列名,因此,如果我们知道列名,我们可能想要检查某个特定列是否存在于数据框中。为此,我们可以使用grep函数,如果列名存在于数据框中,则返回列名,否则返回0。要了解其工作原理,请查看下面的示例。
示例1
考虑下面的数据框:
Gender<−sample(c("Male","Female"),20,replace=TRUE) Y<−rpois(20,5) df1<−data.frame(Gender,Y) df1
输出
Gender Y 1 Male 7 2 Female 4 3 Male 3 4 Male 3 5 Male 4 6 Female 7 7 Male 10 8 Female 4 9 Female 4 10 Female 6 11 Female 6 12 Female 2 13 Male 6 14 Female 4 15 Female 2 16 Male 5 17 Male 1 18 Male 8 19 Female 5 20 Female 2
检查列“Gender”是否存在于df1中:
示例
grep("Gender",names(df1),value=TRUE)
输出
[1] "Gender"
检查列“Sex”是否存在于df1中:
输出
grep("Sex",names(df1),value=TRUE) character(0)
示例2
ID<−1:20 Weather<−sample(c("Summer","Rainy","Winter"),20,replace=TRUE) df2<−data.frame(ID,Weather) df2
输出
ID Weather 1 1 Summer 2 2 Rainy 3 3 Summer 4 4 Summer 5 5 Winter 6 6 Summer 7 7 Rainy 8 8 Rainy 9 9 Rainy 10 10 Winter 11 11 Winter 12 12 Rainy 13 13 Winter 14 14 Winter 15 15 Summer 16 16 Winter 17 17 Rainy 18 18 Summer 19 19 Winter 20 20 Rainy
检查列“atmosphere”是否存在于df2中:
grep("atmosphere",names(df2),value=TRUE) character(0)
检查列“Weather”是否存在于df2中:
示例
grep("Weather",names(df2),value=TRUE)
输出
[1] "Weather"
广告