4

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.

obruzzi
  • 456
  • 1
  • 4
  • 12
  • Can you provide your dataset as an example and the expected output? copy and paste output of `dput(my_dat)` into your question. This will give better context to future readers so they don't have to imagine what your dataset looks like. – acylam Oct 31 '17 at 16:58
  • Hi @useR. Thanks for the comment. You have right, providing an example would have been easier. Luckily Psidom managed to find the solution! – obruzzi Oct 31 '17 at 20:10
  • FYI, you should still provide an example and expected output even though you accepted an answer as SO is meant to benefit a community, not solely the asker of the question. – acylam Oct 31 '17 at 20:13
  • @useR I updated the post with an example – obruzzi Nov 01 '17 at 07:13
  • Please use `dput(my_dat)` instead of posting it as an image – acylam Nov 01 '17 at 13:31

1 Answers1

12

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

Psidom
  • 209,562
  • 33
  • 339
  • 356