Home > Mobile >  Why don't we have statistics for each group?
Why don't we have statistics for each group?

Time:10-13

Input:

library("UsingR")
library("dplyr")
data("kid.weights")
attach(Kid.weights)
df <- data.frame(gender[1:6],
                 weight[1:6],
                 height[1:6],
                 Kg=weight[1:6] * 0.453592,
                 M=height[1:6] * 0.0254)

df
df %>%
  group_by(df$gender) %>%
  summarise(mean(df$weight))

Output:

> df %>%
    group_by(df$gender) %>%
    summarise(mean(df$weight))
# A tibble: 2 x 2
  `df$gender` `mean(df$weight)`
  <fct>                   <dbl>
1 F                        58.3
2 M                        58.3

I want to make data frame for mean(weight(kg)) or median(weight(kg)) to gender. but it is not working. looks like. how to it solve?

CodePudding user response:

Once you use %>% you don't need to reference to df anymore:

df %>%
  group_by(gender) %>%
  summarise(mean(weight))

%>% is a pipeline which makes you accessible to the columns directly, on each group, df$gender and df$weight would give you the whole column.

  • Related