简体   繁体   English

在R中一次从数据框中的多个变量中删除Na

[英]Remove Na's From multiple variables in Data Frame at once in R

So I have a data frame: df and I plot it but there are too many Na's and it is not nice.所以我有一个数据框:df 并且我绘制了它,但是 Na 太多了,这不好。

So I try to remove Na's with 1):所以我尝试用 1) 删除 Na:

 df <- na.omit(df)

But my data are getting messed up.但是我的数据变得一团糟。 2): 2):

 df <- df[!is.na(df$column_name),]

This work for a specific column name but in the plot I have multiple column names with Na's and when I try to use the same command but for other column name it changes my data complitely.这适用于特定的列名,但在图中我有多个带有 Na 的列名,当我尝试使用相同的命令但对于其他列名时,它会完全更改我的数据。 So can anyone help me?那么有人可以帮助我吗? Is there a way to !is.na (Multiple column names) Or Ignore NA's In a ggplot?有没有办法在!is.na使用!is.na (多列名称)或忽略 NA?

I am using this:我正在使用这个:

df<-Ass1MatrixNoNa %>% gather(test, value, 3:5)
ggplot(df,aes(x=test,fill=value)) +
  geom_bar(position=position_dodge(preserve="single"))

在此处输入图片说明

And I get a plot but with NA's我得到了一个情节,但有 NA 的
Then I try to remove the NA's:然后我尝试删除 NA:

Ass1MatrixNoNa <- Ass1Matrix[!is.na(Ass1Matrix$Ass_1_hearingA),]

Removes the Ass_1_hearingA Na's But I want also hearingB but ovverides the first one and the NA's are removed only in the second one:删除 Ass_1_hearingA Na's 但我也想要 soundB 但覆盖第一个,NA's 仅在第二个中删除:

Ass1MatrixNoNa <- Ass1Matrix[!is.na(Ass1Matrix$Ass_1_hearingB]

I'm unsure if this is what you want.我不确定这是否是你想要的。 But if you are trying to deal with warnings from geom_bar regarding NA s, you may notice from the documentation ( help("geom_bar") ) that that the function has the argument na.rm .但是,如果您试图处理来自geom_bar关于NAwarnings ,您可能会从文档( help("geom_bar") )中注意到该函数具有参数na.rm So the function can remove the NA s for you.因此该函数可以为您删除NA Try尝试

ggplot(df,aes(x=test,fill=value)) +
  geom_bar(position=position_dodge(preserve="single"), na.rm = TRUE)

Does that do what you want?这样做你想要的吗?

So you may not necessarily need to remove the NAs in df .因此,您可能不一定需要删除df中的 NA。

EDIT : Otherwise, the complete.cases function might help you:编辑:否则, complete.cases功能可能会帮助您:

df <- data.frame(x = c(1, NA, 3, 4), value = c(1, 2, 3, 4), fill= c(1, 2, NA, 4))
print(df)
#   x value fill
#1  1     1    1
#2 NA     2    2
#3  3     3   NA
#4  4     4    4

ccol <- c("value", "fill") # Cols to keep 'complete'
df_complete <- df[complete.cases(df[, ccol]), ]
print(df_complete)
#   x value fill
#1  1     1    1
#2 NA     2    2
#4  4     4    4

Running complete.cases(x) returns a logical vector with TRUE where no NA s appear in the rows of x .运行complete.cases(x)返回一个逻辑向量,其值为TRUE ,其中x的行中没有出现NA

Alternatively, using the tidyverse / dplyr , something like the following或者,使用tidyverse / dplyr ,类似于以下内容

df_complete2 <- df %>% filter(!is.na(fill) & !is.na(value))

should do it too.也应该这样做。

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

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