简体   繁体   English

如何删除R中数据框特定列中的NA值?

[英]How to remove NA values in a specific column of a dataframe in R?

I have a data frame with a large number of observations and I want to remove NA values in 1 specific column while keeping the rest of the data frame the same.我有一个包含大量观察值的数据框,我想删除 1 个特定列中的 NA 值,同时保持数据框的其余部分相同。 I want to do this without using na.omit().我想在不使用 na.omit() 的情况下执行此操作。 How do I do this?我该怎么做呢?

We can use is.na or complete.cases to return a logical vector for subset ting我们可以使用is.nacomplete.casessubset返回一个逻辑向量

subset(df1, complete.cases(colnm))

where colnm is the actual column name其中colnm是实际的列名

This is how I would do it using dplyr :这就是我使用dplyr

library(dplyr) 

df <- data.frame(a = c(1,2,NA), 
                 b = c(5,NA,8))
filter(df, !is.na(a))

# output 
a  b
1 1  5
2 2 NA

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

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