0
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.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Robert Cline
  • 37
  • 1
  • 7

1 Answers1

1

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).

walter
  • 518
  • 3
  • 8