R语言 在考虑类别和子类别的情况下,每隔16行创建一个分页符

fykwrbwg  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(114)

我面临着一个挑战,在创建分页后,每16行考虑类别和子类别。假设我们有如下的数据,其中category是row_label1,sub_category是row_label2
我已经为每16行创建了一个组作为增量。

structure(list(row_label1 = c("Age (years)", "Age (years)", "Age (years)", 
"Age (years)", "Age range, n(%)", "Age range, n(%)", "Age range, n(%)", 
"Race, n(%)", "Race, n(%)", "Race, n(%)", "Gender, n(%)", "Gender, n(%)", 
"Ethnicity, n(%)", "Ethnicity, n(%)", "Height (cm)", "Height (cm)", 
"Height (cm)", "Height (cm)", "Weight (kg)", "Weight (kg)", "Weight (kg)", 
"Weight (kg)", "Body Mass Index (kg/m2)", "Body Mass Index (kg/m2)", 
"Body Mass Index (kg/m2)", "Body Mass Index (kg/m2)", "BMI (kg/m2)", 
"BMI (kg/m2)", "BMI (kg/m2)"), row_label2 = c("n", "Mean (SD)", 
"Median", "Min, Max", "<65", "65-80", ">80", "WHITE", "BLACK OR AFRICAN AMERICAN", 
"AMERICAN INDIAN OR ALASKA NATIVE", "Female", "Male", "Hispanic or latino", 
"Not hispanic or latino", "n", "Mean (SD)", "Median", "Min, Max", 
"n", "Mean (SD)", "Median", "Min, Max", "n", "Mean (SD)", "Median", 
"Min, Max", "<25", "25-<30", ">=30"), groups = c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(NA, -29L
), class = c("tbl_df", "tbl", "data.frame"))

我想创建一个新的变量,也考虑到类别和子类别,然后递增的数量,使类别和子类别之间不分裂2页

现在我想有新的变量增加考虑到16行,但与类别和子类别不应该分裂。因此,在新变量中,数字2应该在Height之前开始,尽管考虑到类别和子类别,它缺少16行
我期待一个页面变量的东西如下

eh57zj3b

eh57zj3b1#

关于this post

library(dplyr)
df %>%
  mutate(n=if_else(row_number()==1,n(),0L),.by=row_label1) %>%
  mutate(cumsum_16 = purrr::accumulate(n, ~ifelse(.x + .y <= 16, .x + .y, .y)),
         pageno= cumsum(n == cumsum_16))

# A tibble: 20 x 6
   row_label1      row_label2                       groups     n cumsum_16   pageno
   <chr>           <chr>                             <int> <int>     <int>    <int>
 1 Age (years)     n                                     1     4         4        1
 2 Age (years)     Mean (SD)                             1     0         4        1
 3 Age (years)     Median                                1     0         4        1
 4 Age (years)     Min, Max                              1     0         4        1
 5 Age range, n(%) <65                                   1     3         7        1
 6 Age range, n(%) 65-80                                 1     0         7        1
 7 Age range, n(%) >80                                   1     0         7        1
 8 Race, n(%)      WHITE                                 1     3        10        1
 9 Race, n(%)      BLACK OR AFRICAN AMERICAN             1     0        10        1
10 Race, n(%)      AMERICAN INDIAN OR ALASKA NATIVE      1     0        10        1
11 Gender, n(%)    Female                                1     2        12        1
12 Gender, n(%)    Male                                  1     0        12        1
13 Ethnicity, n(%) Hispanic or latino                    1     2        14        1
14 Ethnicity, n(%) Not hispanic or latino                1     0        14        1
15 Height (cm)     n                                     1     4         4        2
16 Height (cm)     Mean (SD)                             1     0         4        2
17 Height (cm)     Median                                2     0         4        2
18 Height (cm)     Min, Max                              2     0         4        2
19 Weight (kg)     n                                     2     4         8        2
20 Weight (kg)     Mean (SD)                             2     0         8        2
...

相关问题