Search code examples
rdplyrsummarize

Summarise_each for first non-NA value


Is there a way to instruct dplyr to use summarise_each with specification first and na.rm=TRUE?

I have a dataframe with many NAs and numeric values. Column A is patient ID. I would like to summarise the dataframe according to patient ID by taking the first non-NA of each variables. This didn't work

`summarised_df <- df %>% group_by(patient_ID) %>%
  summarise_each(funs(first(., na.rm=TRUE)))`

Thanks in advance!

Here you can find an example of the data. However, the original data includes hundreds of different variables.


Solution

  • You can use first(na.omit(.)) or na.omit(.)[1]. Besides summarise_each is deprecated, use summarise_all instead.