0

I'm currently working with a data frame where I wish to summarize drug dosage per day per patient. With the tidyverse I tried using a

df%>%
group_by (patient, date)%>%
summarise(doseperday = aggregate(drugdose)

but no luck.

I.e.

|patient|drugdose|date    |
|Bob    |5       |10/10/18|
|Bob    |3       |10/10/18|
|Bob    |4       |10/11/18|
|Bob    |1       |10/11/18|
|Jim    |5       |10/10/18|
|Jim    |8       |10/10/18|
|Jim    |3       |10/11/18|
|Jim    |1       |10/11/18|

I want to get the following:

|patient|drugdose|date    |
|Bob    |8       |10/10/18|
|Bob    |5       |10/11/18|
|Jim    |12      |10/10/18|
|Jim    |4       |10/11/18|

Thanks for the help.

ragzoxaim
  • 11
  • 5
  • 1
    Use `sum` instead of `aggregate`, plus you are missing one parenthesis – PKumar Oct 28 '19 at 02:30
  • Adding to the client above, make sure to use `ungroup()` afterward. – Phil Oct 28 '19 at 03:51
  • 2
    Dplyr solution df2 <- df%>% group_by (patient, date)%>% summarise(doseperday = sum(drugdose)) %>% ungroup() df2 Base R solution df2 <- aggregate(drugdose~patient+date, df, sum) – hello_friend Oct 28 '19 at 04:20

0 Answers0