Grouped Summary Not Working

I'm not sure what is incorrect but R only returns one value when running this code
combinesub4<-group_by_at(combinesub3,vars(USUBJID,PARAMCD))
combinesub5<-summarize(combinesub4,AVAL=mean(AVAL,na.rm=TRUE))

I also used the "plain" group_by as well with the same result (one value returned, no grouped summary results)

Please provide a small reproducible example, just so we can understand with what data you work and what you try to do (maybe one value is actually correct because of your data).

Try this as example data. There are actually more groups by which to summarize but let's start with two.

USUBJID PARAMCD AVAL
1 A 4
1 A 5
1 B 3
2 A 2
2 A 1
2 B 2
2 B 3

I cannot replicate the problem:

data <- data.frame(
  USUBJID = c(rep(1,3),rep(2,4)),
  PARAMCD = c('A','A','B','A','A','B','B'),
  AVAL = c(4,5,3,2,1,2,3)
)
library(dplyr)
data |> 
  group_by(USUBJID,PARAMCD) |>
  summarise(AVAL=mean(AVAL,na.rm=TRUE))

#> # A tibble: 4 × 3
#> # Groups:   USUBJID [2]
#>   USUBJID PARAMCD  AVAL
#>     <dbl> <chr>   <dbl>
#> 1       1 A         4.5
#> 2       1 B         3  
#> 3       2 A         1.5
#> 4       2 B         2.5

# your method
combinesub4<-group_by_at(data,vars(USUBJID,PARAMCD))
combinesub5<-summarise(combinesub4,AVAL=mean(AVAL,na.rm=TRUE))

combinesub5
#> # A tibble: 4 × 3
#> # Groups:   USUBJID [2]
#>   USUBJID PARAMCD  AVAL
#>     <dbl> <chr>   <dbl>
#> 1       1 A         4.5
#> 2       1 B         3  
#> 3       2 A         1.5
#> 4       2 B         2.5

Created on 2022-08-30 by the reprex package (v2.0.1)

All means are correctly calculated by groups.

I cannot replicate your results - it returns one value

 data <- data.frame(
+     USUBJID = c(rep(1,3),rep(2,4)),
+     PARAMCD = c('A','A','B','A','A','B','B'),
+     AVAL = c(4,5,3,2,1,2,3)
+ )
> data |> 
+     group_by(USUBJID,PARAMCD) |>
+     summarise(AVAL=mean(AVAL,na.rm=TRUE))
      AVAL
1 2.857143
> View(data)

What libraries are currently loaded in your session? Maybe there is another function masking dplyr::group_by() or dplyr::summarise()? Try to add dplyr:: in front of group_by() and summarise() to see if there is only one value returned again.

Does this code show that you typed AVAL as a line of code and submitted it to look at the value, or is the idea that AVAL 1 2.857143 appeared as a direct and immediate result of the data and pipe group_by summarise functions ?

Ok that seems to have worked. It must be a masking thing.
The libraries I have loaded are:
base
dplyr
datasets
forcats
foreign
ggplot2
gsubfn
haven
methods
plyr
purrr
readr
rsqlite
sqldf
stats
stringr
tidyr
tibble
tidyverse
utils

Happy that I could help.
Please accept the answer solving the issue to indicate, that a soultion was found.

You can see which package causes the issue, if you write ?group_by (or ?summarise) in your console and have a look which functions pop up in the documentations. Additionally, you may consider rethinking your packages you load. E.g. dplyr is inside tidyverse, as well as tidyr is. So it's pointless to load tidyverse with dplyr and tidyr.

Kind regards

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.