可以使用colnames函数以及该complete.cases函数来提取不缺少值的列名。该complete.cases函数将提取不缺少值的列,然后列名将仅提取那些列名。
考虑以下数据帧-
x1<−sample(c(NA,1,0),20,replace=TRUE) x2<−rpois(20,5) x3<−sample(c(NA,5,10),20,replace=TRUE) df1<−data.frame(x1,x2,x3) df1输出结果
x1 x2 x3 1 1 1 5 2 NA 9 NA 3 0 5 5 4 NA 3 NA 5 0 2 10 6 NA 4 10 7 0 4 NA 8 NA 5 5 9 0 3 10 10 1 1 5 11 0 2 5 12 1 2 10 13 1 7 10 14 NA 8 5 15 1 1 NA 16 NA 3 NA 17 0 9 5 18 0 5 5 19 1 7 NA 20 1 4 10
提取没有缺失值的列名-
colnames(df1)[complete.cases(t(df1))]输出结果
[1] "x2"
y1<−letters[1:20] y2<−sample(c(NA,"A","B","C"),20,replace=TRUE) y3<−sample(c(NA,rnorm(5)),20,replace=TRUE) df2<−data.frame(y1,y2,y3) df2输出结果
y1 y2 y3 1 a A 1.2176106 2 b A NA 3 c A 0.7633109 4 d <NA> −0.3289873 5 e C 0.7633109 6 f C −0.3289873 7 g C −0.9952887 8 h <NA> 1.2176106 9 i B 1.2176106 10 j <NA> −0.9952887 11 k <NA> NA 12 l A 0.6808376 13 m B 0.7633109 14 n B −0.9952887 15 o C 0.7633109 16 p A NA 17 q C 1.2176106 18 r C −0.9952887 19 s A 0.7633109 20 t B 1.2176106
提取没有缺失值的列名-
colnames(df2)[complete.cases(t(df2))]输出结果
[1] "y1"