Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用列表将组_行馈送到kable中_R_Kable - Fatal编程技术网

R 使用列表将组_行馈送到kable中

R 使用列表将组_行馈送到kable中,r,kable,R,Kable,我正在尝试使用R上的kable生成一个大表。 我想使用存储在对象中的名称对一些行进行分组,但找不到如何进行分组 这就是我所拥有的: kable(Baseline, "html") %>% kable_styling("striped") %>% add_header_above(c(" " = 1, "My score" = 4)) %>% group_rows(index= c(" "=3, "Age" = 4, "BMI" = 4)) 在本例中,我只有3个类别

我正在尝试使用R上的kable生成一个大表。 我想使用存储在对象中的名称对一些行进行分组,但找不到如何进行分组

这就是我所拥有的:

kable(Baseline, "html") %>%
  kable_styling("striped") %>%
  add_header_above(c(" " = 1, "My score" = 4)) %>%
  group_rows(index= c(" "=3, "Age" = 4, "BMI" = 4))
在本例中,我只有3个类别对行进行子分类,但实际上我有更多类别,因此我想知道是否可以调用包含名称和要包含的行数的对象,而不是编写每个因子,如:

kable(Baseline, "html") %>%
  kable_styling("striped") %>%
  add_header_above(c(" " = 1, "My score" = 4)) %>%
  group_rows(index= nametable)
其中,nametable包含与此名称对应的名称和行数

希望这是清楚的


感谢您使用iris数据集:

library(tidyverse)
library(kableExtra)

# split the dataset, creating a df for each category from the variable you'll use to group the rows
split_Table <- iris %>%
                split(.$Species) %>%
                discard(function(x) nrow(x) == 0) %>%
                map(., ~ select(., -Species)) # remove Species from split dfs

num_rows <- map(1:length(names(split_Table)), function(x){
             nrow(split_Table[[x]]) 
            }) %>% unlist()  # create function to count frequency of each category

split_Table %>%
 bind_rows() %>%
 kable("html", align = "c") %>%
 kable_styling(full_width = T) %>%
 group_rows(index = setNames(num_rows, names(split_Table)),
            label_row_css = "background-color: #666; color: #fff;") 
# create table with grouped rows
希望这有帮助

稍微短一点

library(tidyverse)
library(kableExtra)

counts <- table(iris$Species)

iris %>%
  arrange(Species)  %>%  # same order as table results  
  select(-Species)  %>%
  kable("html", align = "c") %>%
  kable_styling(full_width = T) %>%
  group_rows(index = setNames(counts, names(counts)),
             label_row_css = "background-color: #666; color: #fff;")