Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summarise_each for first non-NA value

Tags:

r

dplyr

summarize

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.

like image 546
obruzzi Avatar asked Dec 19 '22 03:12

obruzzi


1 Answers

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

like image 179
Psidom Avatar answered Jan 10 '23 10:01

Psidom