`summarise()` has grouped output by 'TenYearCHD'. You can override using the `.groups` argument.

plot_sexCHD = heart%>%
group_by(TenYearCHD, male)%>%
summarise(cnt = n())%>%
mutate(pcnt = cnt/sum(cnt))%>%
ggplot(aes(x = as.factor(male), y = pcnt, fill = TenYearCHD))+
geom_bar(col = "white", position = "dodge", stat = "identity")+
labs(x = "Sex", y = "Percentage", title = 'Male Female Percentage of CHD')+
scale_fill_brewer(palette = "Blues")

str(heart)
'data.frame': 4238 obs. of 16 variables:
male : int 1 0 1 0 0 0 0 0 1 1 ... age : int 39 46 48 61 46 43 63 45 52 43 ...
education : int 4 2 1 3 3 2 1 2 1 1 ... currentSmoker : int 0 0 1 1 1 0 0 1 0 1 ...
cigsPerDay : int 0 0 20 30 23 0 0 20 0 30 ... BPMeds : int 0 0 0 0 0 0 0 0 0 0 ...
prevalentStroke: int 0 0 0 0 0 0 0 0 0 0 ... prevalentHyp : int 0 0 0 1 0 1 0 0 1 1 ...
diabetes : int 0 0 0 0 0 0 0 0 0 0 ... totChol : int 195 250 245 225 285 228 205 313 260 225 ...
sysBP : num 106 121 128 150 130 ... diaBP : num 70 81 80 95 84 110 71 71 89 107 ...
BMI : num 27 28.7 25.3 28.6 23.1 ... heartRate : int 80 95 75 65 85 77 60 79 76 93 ...
glucose : int 77 76 70 103 85 99 85 78 79 88 ... TenYearCHD : int 0 0 0 1 0 0 1 0 0 0 ...

The message you quote in your title is normal and not a cause for concern. I get a similar message in the code below. Is there a problem with the result of your code?

library(dplyr)
DF <- data.frame(G = c("A", "A", "A", "A", "B", "B", "B", "B"),
                  G2 = c("X", "X", "Y", "Y", "X", "X", "Y", "Y"),
                  Val = 1:8)
DF |> group_by(G, G2) |> summarize(S = sum(Val))
`summarise()` has grouped output by 'G'. You can override using the `.groups` argument.
# A tibble: 4 x 3
# Groups:   G [2]
  G     G2        S
  <chr> <chr> <int>
1 A     X         3
2 A     Y         7
3 B     X        11
4 B     Y        15

Most of the time you don't care that the result is grouped
It happens when you have more than one column in group_by
A while ago the option .groups = "drop" was added

I forget the circumstance (maybe when you try to select a few columns from the result) but R insists on adding back the grouped columns even though not in select.

I remember reading a comment that a better default would have been no grouping, but dplyr had been out there some time before v1.0.0 added the new options

This topic was automatically closed 21 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.