1

I'm trying to see if R has a command similar to Stata. In Stata, the !mi(a, b, c,...) command creates a new variable and indicates a 1/0 if the indicated variable(s) have no missing data. 1 = no missing data across variables x, 0 = missing data in one of the variables x.

I'm looking for a simple code because sometimes I have about 15-20 variables (mainly to mark listwise deletion cases). It takes a little more work but I specify the column names instead of using the : marker. The options I've found creates a new dataframe (na.omit), but I want to retain all the cases.

I know that ifelse can accomplish this using:

df$test <- ifelse(!is.na(df$ID) & !is.na(df$STATUS), 1,0)

I like to know if there's another way with less code where I don't need to write "!is.na(df$ )" over and over. Maybe a $global code (similar to Stata)?

0

2 Answers 2

3

You should be able to do this using complete.cases

df$test <- as.numeric(complete.cases(df))
Sign up to request clarification or add additional context in comments.

1 Comment

And you can specify certain columns by picking them out of df: as.numeric(complete.cases(df[, c("a", "b", "d")])
0

You could also use rowSums:

df$test <- as.numeric(rowSums(is.na(df)) == 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.