[英]Filter remove NA string or value from tibble
Here I'm attempting to remove NA values from a tibble : 在这里,我尝试从小标题中删除NA值:
mc = as_tibble(c("NA" , NA , "ls", "test"))
mc <- filter(mc , is.na == TRUE)
But error is returned : 但是返回错误:
> mc = as_tibble(c("NA" , NA , "ls", "test"))
> mc <- filter(mc , is.na == TRUE)
Error in filter_impl(.data, quo) :
Evaluation error: comparison (1) is possible only for atomic and list types.
How to remove NA values from this tibble ? 如何从此小标题中删除NA值?
Try: 尝试:
library(tidyverse)
mc %>%
mutate(value = replace(value, value == "NA", NA)) %>%
drop_na()
Which gives: 这使:
# A tibble: 2 x 1
value
<chr>
1 ls
2 test
Second line replaces all "NA"
to a real <NA>
. 第二行将所有
"NA"
替换为真实的<NA>
。 Then the third line drops all <NA>
values. 然后,第三行删除所有
<NA>
值。
If you simply want to remove actual NA values: 如果您只想删除实际的NA值:
library(dplyr)
filter(mc, !is.na(value))
Alternatively (this will check all columns, not just the specified column as above): 或者(这将检查所有列,而不仅仅是上面指定的列):
na.omit(mc)
If you want to remove both NA values, and values equaling the string "NA": 如果要删除两个NA值,以及等于字符串“ NA”的值:
library(dplyr)
filter(mc, !is.na(value), !value == "NA")
The solutions given by @tyluRp and @danh work perfectly fine. @tyluRp和@danh给出的解决方案可以很好地工作。
Just wanted to add another alternative solution with the advantages 只是想添加另一种具有优势的替代解决方案
See this one-liner: 看到此一线:
mc %>% replace(. == "NA", NA) %>% na.omit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.