Jeremy K.
Jeremy K.

Reputation: 1792

NA values causing problems in summarise() even when using rm.na = TRUE

I'm trying to take the mean of some data with NA values, and I would like the NA values to be ignored. A reproducible example would be:

     country      gdp
1    Austria    25.17
2 Azerbaijan       NA
3 Bangladesh    27.79
4    Belarus       NA
testdf2 <- data.frame(stringsAsFactors=FALSE,
     country = c("Austria", "Azerbaijan", "Bangladesh", "Belarus"),
         gdp = c(25.17654, NA, 27.7971, NA)
)

I've tried summarise() using rm.na = TRUE and without

library(dplyr)
testdf2 %>% summarise(gdp_mean = mean(gdp))

testdf2 %>% summarise(gdp_mean = mean(gdp), rm.na = TRUE)

but I keep getting output that looks like this:

  gdp_mean
1       NA

Can anyone tell me what I'm doing wrong, please?

Upvotes: 0

Views: 57

Answers (1)

ricoderks
ricoderks

Reputation: 1619

I think you made a typo. I tried your code like this and it works: rm.na should be na.rm, and of course what @kwiscion mentioned in his comment!

testdf2 %>% summarise(gdp_mean = mean(gdp, na.rm = TRUE))

Upvotes: 1

Related Questions