5

各グループのレコード数を報告し、一連の変数の平均も表示するグループ化された要約を作成しようとしています。

これを 2 つの別々の要約としてまとめて結合する方法しか考えられません。これは問題なく動作しますが、これを行うためのよりエレガントな方法があるのだろうか?

dailyn<-daily %>% # this summarises n
  group_by(type) %>%
  summarise(n=n()) %>%

dailymeans <- daily %>% # this summarises the means
  group_by(type) %>%
  summarise_at(vars(starts_with("d.")),funs(mean(., na.rm = TRUE))) %>%

dailysummary<-inner_join(dailyn,dailymeans) #this joins the two parts together

私が扱っているデータは、次のようなデータフレームです。

daily<-data.frame(type=c("A","A","B","C","C","C"),
                  d.happy=c(1,5,3,7,2,4),
                  d.sad=c(5,3,6,3,1,2))
4

2 に答える 2