0

I want to remove all of the NA’s from the variables selected however when I used na.omited() for example:

na.omit(df$livharm) 

it does not work and the NA’s are still there. I have also tried an alternative way for example:

married[is.na(livharm1)] <-NA 

I have done this for each variable within the larger variable I am looking at using the code: E.g.

df <- within(df, { 
married <- as.numeric(livharm == 1) 
“
“
“ 

married[is.na(livharm1)] <- NA

})

however I’m not sure what I actually have to do. Any help I would greatly appreciate!

Nad Pat
  • 3,129
  • 3
  • 10
  • 20
  • Does this answer your question? [Remove rows with all or some NAs (missing values) in data.frame](https://stackoverflow.com/questions/4862178/remove-rows-with-all-or-some-nas-missing-values-in-data-frame) – user438383 Nov 14 '21 at 11:32
  • Hi, I’ve tried these however it runs the code correctly yet when I go to use ggplot it still shows the NA results within the graph as well as still showing them within a table when the summary command in r studio. Thank you for coming back to me :) – Anonymous_tech Nov 14 '21 at 11:58
  • Are you sure that your NA values are really NA? They may also be character or factor. – maydin Nov 14 '21 at 12:05
  • @Anonymous_tech can you please share your data by pasting the output of ``dput(data)`` into your original question. Thanks. – user438383 Nov 14 '21 at 12:12
  • Thank you!! :)) – Anonymous_tech Nov 15 '21 at 20:06

1 Answers1

1

Using complete.cases gives:

dat <- data.frame( a=c(1,2,3,4,5),b=c(1,NA,3,4,5) )

dat
  a  b
1 1  1
2 2 NA
3 3  3
4 4  4
5 5  5

complete.cases(dat)
[1]  TRUE FALSE  TRUE  TRUE  TRUE

# is.na equivalent has to be used on a vector for the same result:
!is.na(dat$b)
[1]  TRUE FALSE  TRUE  TRUE  TRUE

dat[complete.cases(dat),]
  a b
1 1 1
3 3 3
4 4 4
5 5 5

Using na.omit is the same as complete.cases but instead of returning a boolean vector the object itself is returned.

na.omit(dat)
  a b
1 1 1
3 3 3
4 4 4
5 5 5

This function returns a different result when applied only to a vector, which is probably not handled correctly by ggplot2. It can be "rescued" by putting it back in a data frame. base plot works as intended though.

na.omit(dat$b)
[1] 1 3 4 5
attr(,"na.action")
[1] 2
attr(,"class")
[1] "omit"

data.frame(b=na.omit(dat$b))
  b
1 1
2 3
3 4
4 5

Plotting with ggplot2

ggplot(dat[complete.cases(dat),]) + geom_point( aes(a,b) )
# <plot>

# See warning when using original data set with NAs
ggplot(dat) + geom_point( aes(a,b) )
Warning message:
Removed 1 rows containing missing values (geom_point).
# <same plot as above>
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29