如何构建摘要数据框

How to build a summary data frame

我的数据集如下所示:

我想得到一个汇总数据集,如下所示:

我该怎么办?谢谢。 sample.data可以通过以下代码构建:

ID<- c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18")
Group<-c("A","B","C","D","D","D","A","B","D","C","B","D","A","A","C","B","B","B")
Color<-c("Green","Yellow","Red","Red","Red","Yellow","Green","Green","Yellow","Red","Red","Yellow","Yellow","Yellow","Green","Red","Red","Green")
Realy_Love<-c("Y","N","Y","Y","N","N","Y","Y","Y","N","N","Y","N","Y","N","Y","N","Y")
Sample.data <- data.frame(ID, Group, Color, Realy_Love)

您可以使用 dplyr 并按以下项目分组:

Sample.data %>%
  group_by(Group, Color, Realy_Love) %>%
  summarise(Obs = n())

# Group Color  Realy_Love   Obs
# <chr> <chr>  <chr>      <int>
# 1 A     Green  Y              2
# 2 A     Yellow N              1
# 3 A     Yellow Y              1
# 4 B     Green  Y              2
# 5 B     Red    N              2
# 6 B     Red    Y              1
# 7 B     Yellow N              1
# 8 C     Green  N              1
# 9 C     Red    N              1
# 10 C     Red    Y              1
# 11 D     Red    N              1
# 12 D     Red    Y              1
# 13 D     Yellow N              1
# 14 D     Yellow Y              2

使用 Tidyverse 中的 dplyr 获取摘要。然后,您可以使用 arrange() 按颜色或其他变量排序。

  group_by(Group, Color, Realy_Love) %>% 
  summarise(Obs = n()) %>% 
  arrange(Color) 

使用 dplyr,您甚至不需要对列进行分组,只需使用 count() 函数一步解决:

Sample.data %>%
    count(Group, Color, Realy_Love, sort = TRUE)

可选的 sort = TRUE 参数表示从最频繁出现的降序排列:

   Group  Color Realy_Love n
1      A  Green          Y 2
2      B  Green          Y 2
3      B    Red          N 2
4      D Yellow          Y 2
5      A Yellow          N 1
6      A Yellow          Y 1
7      B    Red          Y 1
8      B Yellow          N 1
9      C  Green          N 1
10     C    Red          N 1
11     C    Red          Y 1
12     D    Red          N 1
13     D    Red          Y 1
14     D Yellow          N 1