简体   繁体   English

在R中删除NA中的列值

[英]Remove column values with NA in R

I have a data frame, called gen, which is represented below 我有一个名为gen的数据框,如下所示

     A       B       C       D       E
1    NA      4.35    35.3    3.36    4.87
2    45.2    .463    34.3    NA      34.4
3    NA      34.5    35.6    .457    46.3

I would like to remove the columns where there are NA's. 我想删除有NA的 (I know na.omit does it for rows, but I can't seem to find one for columns). (我知道na.omit用于行,但似乎找不到用于列的行)。 The final result would read: 最终结果如下:

     B       C       E
1    4.35    35.3    4.87
2    .463    34.3    34.4
3    34.5    35.6    46.3

Thanks! 谢谢!

gen <- gen[sapply(gen, function(x) all(!is.na(x)))]
dfrm[ , sapply(dfrm, function(x){ !any(is.na(x)) } )

You might want to use instead this variant: 您可能想要使用此变体:

dfrm[ , sapply(dfrm, function(x){ all(is.finite(x)) } )

If you have Inf or -Inf values in a vector they are not removed or identified with selection based on is.na . 如果向量中包含Inf或-Inf值,则不会根据is.na删除或识别is.na

只要用这个:

gen[colSums(is.na(gen)) == 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM