简体   繁体   中英

How to remove rows with ? mark instead of NA in R

I have a dataframe in r, but instead of NA, there is question mark. So using na.omit doesn't work. How can i remove rows having ? in it.

Thanks

dat <- data.frame(v1 = c(1, 5, "?"), v2 = c(3, 3, 9))
dat[dat$v1 != "?",]
  v1 v2
1  1  3
2  5  3

rowSums

df[rowSums(df=="?")>0,]
> a <- c(1:5)
> b <- c(2:5,"?")
> c <- c("a","b","?","d","e")
> 
> df <- data.frame(a,b,c)
> df
  a b c
1 1 2 a
2 2 3 b
3 3 4 ?
4 4 5 d
5 5 ? e
> df[df == "?"] <- NA
> df
  a    b    c
1 1    2    a
2 2    3    b
3 3    4 <NA>
4 4    5    d
5 5 <NA>    e

then you can use na.omit()

Another option is converting the question mark to NA using na_if :

library(dplyr)
df %>%
  na_if("?") %>%
  na.omit()

Output:

  v1 v2
2  d  c

Data

df <- data.frame(v1 = c("?", "d"),
                 v2 = c("e", "c"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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