Robert Cline
Robert Cline

Reputation: 37

How do you remove NAs?

penguins %>% 
  select(bill_length_mm) %>% 
filter(!is.na(bill_length_mm)) %>% 
  mean(as.numeric(bill_length_mm), na.rm=TRUE)

Error message: argument is not numeric or logical: returning NA[1] NA

Examining the dataset after filtering shows numeric data with no NAs.
Why am I getting this error? Shouldn't line two or line three of the code remove all NAs?

Thank you.

Upvotes: 0

Views: 93

Answers (1)

walter
walter

Reputation: 528

Try wrapping mean in summarize, like

penguins %>% 
  select(bill_length_mm) %>% 
filter(!is.na(bill_length_mm)) %>% 
  summarize(mean(as.numeric(bill_length_mm), na.rm=TRUE))

This is happening because mean is expecting a vector, but you are passing a dataframe (from the pipe).

Upvotes: 1

Related Questions