在Rmarkdown中对列进行分组

时间:2019-03-04 20:42:44

标签: r r-markdown kable kableextra

我正在尝试生成一个分组表,类似于this。如果我的组是由天气变量组成的月份,而行是年份,则无法弄清楚它是如何工作的。

这是数据的样子:

     year_var month  temp  rain  rhum
      <dbl> <dbl> <dbl> <dbl> <dbl>
 1     2007     4  11    16.4  73.5
 2     2007     5  11.6  38.3  74.3
 3     2007     6  14   108.   83.9
 4     2007     7  14.4 129.   83.6
 5     2007     8  14.9 104.   83.2
 6     2007     9  13.5  36.8  82.7
 7     2008     4   7.8  31.8  76.4
 8     2008     5  12.7  37.1  76.6
 9     2008     6  13.3  86.8  77.7
10     2008     7  15.2 137.   80.4
11     2008     8  15.3 142.   83.1
12     2008     9  12.4  81.2  84.9

是否可以将month列转换为分组变量?

我的数据示例:

structure(list(year_var = c(2007, 2007, 2007, 2007, 2007, 2007, 
2008, 2008, 2008, 2008, 2008, 2008), month = c(4, 5, 6, 7, 8, 
9, 4, 5, 6, 7, 8, 9), temp = c(11, 11.6, 14, 14.4, 14.9, 13.5, 
7.8, 12.7, 13.3, 15.2, 15.3, 12.4), rain = c(16.4, 38.3, 107.7, 
129.3, 103.8, 36.8, 31.8, 37.1, 86.8, 136.7, 142.4, 81.2), rhum = c(73.5, 
74.3, 83.9, 83.6, 83.2, 82.7, 76.4, 76.6, 77.7, 80.4, 83.1, 84.9
)), row.names = c(NA, -12L), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), vars = "year_var", drop = TRUE, indices = list(
    0:5, 6:11), group_sizes = c(6L, 6L), biggest_group_size = 6L, labels = structure(list(
    year_var = c(2007, 2008)), row.names = c(NA, -2L), class = "data.frame", vars = "year_var", drop = TRUE))

0 个答案:

没有答案