1

I have a dataset called dietox which has missing values (NA) for the Feed variable. I need to use conditional selection to create a subset of the data for which the rows with missing values are deleted.

The code I tried was:

dietox[!is.NA[dietox$Feed, ] 

... but am not sure if that is right to create a subset.

dput(head(dietox)) 

dietox <- structure(list(Weight = c(26.5, 27.59999, 36.5, 40.29999, 49.09998, 
             55.39999), Feed = c(NA, 5.200005, 17.6, 28.5, 45.200001, 56.900002 ),
             Time = 1:6, Pig = c(4601L, 4601L, 4601L, 4601L, 4601L, 4601L ),
             Evit = c(1L, 1L, 1L, 1L, 1L, 1L), Cu = c(1L, 1L, 1L, 1L, 1L, 1L),
             Litter = c(1L, 1L, 1L, 1L, 1L, 1L)),
            .Names = c("Weight", "Feed", "Time", "Pig", "Evit", "Cu", "Litter"),
            row.names = c(NA, 6L), class = "data.frame")
IRTFM
  • 258,963
  • 21
  • 364
  • 487
Gracie523
  • 31
  • 3
  • Using the `dput` function is the best way. To only get a subset of the data use the `head` function. Ex. `dput(head(dietox))`. You can read more about good examples [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – C. Braun Feb 15 '18 at 21:39
  • 1
    Please review this post to learn how to ask a question with reproducible example (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). You don't have to provide the entire data frame. A subset of the data frame that represent the key characteristic of your real-world data would be sufficient. – www Feb 15 '18 at 21:40
  • For one thing, you were misspelling `is.na`. Arguably this is a typo, but the answer demonstrates a core R paradigm,s and the questions seems well-phrased. – IRTFM Feb 15 '18 at 21:40
  • @Gracie523. You should now delete the two comments that should have been posted in the body of the question with [edit]-ing features. – IRTFM Feb 15 '18 at 21:47

1 Answers1

3

You have the right idea, but is.na is a function and so needs to be used with parenthesis.

dietox[!is.na(dietox$Feed), ]
C. Braun
  • 5,061
  • 19
  • 47