Question

I am working on the data frame called "juul" that can be found in library(ISwR). How can I re-write the following expression: selection= juul[juul$sex==2 & juul$tanner>2,] so that NA is excluded in the criteria?

Was it helpful?

Solution

You can do that with:

selection <- na.omit(juul[juul$sex==2 & juul$tanner>2,])

However, this returns a dataframe with 0 observations. When looking at the result of:

selection <- juul[juul$sex==2 & juul$tanner>2,]

you can see why. The variable juul$testvol has only NA's for this subset. Using na.omit will therefore exclude all the observations from this subset. You can prevent that by excluding the juul$testvol from the subset with:

selection <- na.omit(juul[juul$sex==2 & juul$tanner>2,c(1:5)])

or

selection <- na.omit(juul[juul$sex==2 & juul$tanner>2,-6])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top