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

Time:06-28

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

CodePudding user response:

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

CodePudding user response:

Try it with rowSums

df[rowSums(df=="?")>0,]

CodePudding user response:

> 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()

  •  Tags:  
  • r
  • Related