R-generate a "missing value variable",

I use R to generate examples of how to handle the missing data for the statistics class that I teach. One method requires the generation of "missing binary variable values", with 0 for cases containing missing values, and 1 without missing values. for example

n XYZ 1 4 300 2 2 8 400 4 3 10 500 7 4 18 NA 10 5 20 50 NA 6 NA 1000 5 

I would like to generate a variable M such that

 nm 1 1 2 1 3 1 4 0 5 0 6 0 

This seems to be simple given the ability of R to handle missing values. The closest I found is m <-ifelse(is.na(missguns),0,1) , but all this creates a new whole data matrix with 0 or 1, indicating absence. However, I just need one variable indicating whether the string contains the missing values.

+8
r missing-data dummy-data
source share
1 answer

complete.cases does exactly what you want.

 complete.cases(x) ## [1] TRUE TRUE TRUE FALSE FALSE FALSE 

You can force the use of a numeric or integer:

 as.integer(complete.cases(x)) ## [1] 1 1 1 0 0 0 
+9
source share

All Articles