仅折叠 R 中数据框的第一列,其余列数据完好无损
collapse only the 1st column of dataframe in R with remaining columns data intact
我是 R 的新手,我有一个如下所示的数据框
Gender Not_Hired Division Hired Total %Hired Odds
Male 490 F 31 521 6.0 0.063
Male 193 E 74 267 27.7 0.383
Male 389 D 193 582 33.2 0.496
Male 286 C 167 453 36.9 0.584
Male 289 B 493 782 63.0 1.706
Male 437 A 715 1152 62.1 1.636
Female 442 F 33 475 6.9 0.075
Female 417 E 131 548 23.9 0.314
Female 341 D 183 524 34.9 0.537
Female 546 C 282 828 34.1 0.516
Female 11 B 24 35 68.6 2.182
Female 27 A 124 151 82.1 4.593
我正在寻找类似的输出
我试过下面的代码
output %>%
select(Division, everything()) %>%
group_by(Division) %>% ungroup() %>%
kbl(caption = 'Company wide hiring',
col.names = c('','','Hired','Not Hired','Total','%Hired','Odds(Hired)'),
digits = 3) %>%
kable_styling()
并获得以下输出:
如何从我的输出获得所需的输出。任何 suggestions/ideas?
您似乎想在第一列中使用 collapse_rows
。但是,要为 kable
输出准备数据,请确保将 Division
和 Gender
作为前两列,并使用 arrange
by Division
所以具有共同 Division
的行彼此相邻。
library(magrittr)
library(kableExtra)
df %>%
select(Division, Gender, everything()) %>%
arrange(Division) %>%
kbl(caption = 'Company wide hiring',
col.names = c('','','Hired','Not Hired','Total','%Hired','Odds(Hired)'),
digits = 3) %>%
kable_styling() %>%
collapse_rows(columns = 1)
输出
我是 R 的新手,我有一个如下所示的数据框
Gender Not_Hired Division Hired Total %Hired Odds
Male 490 F 31 521 6.0 0.063
Male 193 E 74 267 27.7 0.383
Male 389 D 193 582 33.2 0.496
Male 286 C 167 453 36.9 0.584
Male 289 B 493 782 63.0 1.706
Male 437 A 715 1152 62.1 1.636
Female 442 F 33 475 6.9 0.075
Female 417 E 131 548 23.9 0.314
Female 341 D 183 524 34.9 0.537
Female 546 C 282 828 34.1 0.516
Female 11 B 24 35 68.6 2.182
Female 27 A 124 151 82.1 4.593
我正在寻找类似的输出
我试过下面的代码
output %>%
select(Division, everything()) %>%
group_by(Division) %>% ungroup() %>%
kbl(caption = 'Company wide hiring',
col.names = c('','','Hired','Not Hired','Total','%Hired','Odds(Hired)'),
digits = 3) %>%
kable_styling()
并获得以下输出:
如何从我的输出获得所需的输出。任何 suggestions/ideas?
您似乎想在第一列中使用 collapse_rows
。但是,要为 kable
输出准备数据,请确保将 Division
和 Gender
作为前两列,并使用 arrange
by Division
所以具有共同 Division
的行彼此相邻。
library(magrittr)
library(kableExtra)
df %>%
select(Division, Gender, everything()) %>%
arrange(Division) %>%
kbl(caption = 'Company wide hiring',
col.names = c('','','Hired','Not Hired','Total','%Hired','Odds(Hired)'),
digits = 3) %>%
kable_styling() %>%
collapse_rows(columns = 1)
输出