0

I want to create a new data frame that omits missing data. My data is 2 independent people giving a score of an event from 1 to 5.

I want to omit any row with missing data from either rater 1 or rater 2 to create a data frame with only scores from both raters.

So that this data frame:

DF <- data.frame("rater 1"=c(1,4,NA,3,2), "rater 2"=c(1,NA,3,2,3))
head(DF)

Looks like this:

DF_omit <- data.frame("rater1"=c(1,3,2), "rater 2"=c(1,2,3))
head(DF_omit)

(ie row 2 and row 3 of DF have been omitted because of missing data from either rater 1 or rater 2)

Thank you

docalexh
  • 25
  • 7

4 Answers4

0

Use complete.cases to create a logical vector, then index the rows of DF using this vector:

DF_omit <- DF[complete.cases(DF), ]

da11an
  • 701
  • 4
  • 8
0

This is it?

DFnew <- na.omit(DF)
sophocles
  • 13,593
  • 3
  • 14
  • 33
0

Remove all rows with NAs from all columns:

Dataset <- df[complete.cases(df[,]),]

Remove all rows with NA's from specific columns:

Dataset <- df[complete.cases(df[,"column.name"]),] # column names that you want to drop NA's
sophocles
  • 13,593
  • 3
  • 14
  • 33
0

There are a few good ways of doing this - which have been well described elsewhere on SO (e.g. here). However, to use your example here:

I think na.omit is probably the simplest option for your purpose:

na.omit(DF)

#   rater.1 rater.2
# 1       1       1
# 4       3       2
# 5       2       3

There's also complete.cases which is a bit longer but allows you to restrict the NA search to specific columns. While this wasn't required in this question, for completeness it might help to know. For example if you only wanted to remove rows with NA in rater.1:

DF[complete.cases(DF$rater.1),]

#   rater.1 rater.2
# 1       1       1
# 2       4      NA
# 4       3       2
# 5       2       3

Also tidyr has drop_na which might be the easiest if you're already operating in the tidyverse and also has the same benefit as using complete.cases:

library(tidyverse)
DF %>% tidyr::drop_na(rater.1)

#   rater.1 rater.2
# 1       1       1
# 2       4      NA
# 3       3       2
# 4       2       3
Dan Adams
  • 4,971
  • 9
  • 28