0

I apologize for the low-level question, I am just starting out in programming.

I have the following tibble (which I know is not tidy) and I would like to compute the mean of the systolic pressure for each subject, using dplyr::group_by and dplyr::summarise():

bp_systolic2 <- tribble(
  ~ subject_id,  ~ time, ~ systolic,
  1,       1,        120,
  1,       2,        118,
  1,       3,        121,
  2,       1,        125,
  2,       2,        131,
  3,       1,        141
) 

I have tried this:

group_by(bp_systolic2, subject_id) %>%
summarise(mean_systolic = mean(systolic))

However, this just returns a 1x1 tibble with the mean of the whole systolic column. How can I return a tibble (presumably 3x1) which contains the mean systolic for each subject?

Helix
  • 103
  • 1
  • 5
  • 1
    I am getting 3 rows. Did you `library(dplyr)`? Or namespace everything with `dplyr::` – dmi3kno May 02 '18 at 20:42
  • @dmi3kno I did; this is very weird. I am currently using Rstudio, I will try to check it in base R. – Helix May 02 '18 at 20:44
  • It works fine for me as well. It might be a good idea to clear your workspace and then try again. – Marcus Campbell May 02 '18 at 20:44
  • 2
    Maybe: [Why does summarize or mutate not work with groups_by when I load `plyr` after `dplyr`?](https://stackoverflow.com/questions/26106146/why-does-summarize-or-mutate-not-work-with-groups-by-when-i-load-plyr-after-d) – Henrik May 02 '18 at 20:44
  • 1
    Yup, using `plyr::summarise` gives you one row and I bet that is what is happening –  May 02 '18 at 20:45
  • @dmi3kno Hmm in base R I am getting 3 rows as well, but still not in R studio... – Helix May 02 '18 at 20:46
  • @dash2 Ah thank you, I substituted dplyr:summarise and it works! – Helix May 02 '18 at 20:48

1 Answers1

0

It seems your usage is correct. This issue usually occurs when summarise from another package is loaded over dplyr. (for instance when you load plyr after dplyr). Can you try to use dplyr::summarise instead of summarise.

MartijnVanAttekum
  • 1,405
  • 12
  • 20