我希望通过变量" Industry"将两个数据帧T2和T4组合在一起。以及一个主标题的每个数据集的列。因此,在最终输出表中,我想要列行业,在一列标题下的三列T2,标题为#34; Executive"和另外三列T4作为一个标题的子列"管理"。
T2
Industry percentage_Yes percentage_No Total_responses
1 ALL 94 % 6 % 117
2 Banking/Financial Services 83 % 17 % 6
3 Chemicals 100 % 0 % 5
4 Consumer Goods 75 % 25 % 8
5 Energy 89 % 11 % 9
6 High Tech 100 % 0 % 8
7 Insurance/Reinsurance 100 % 0 % 14
8 Life Sciences 100 % 0 % 11
9 Logistics -- -- 3
10 Mining & Metals -- -- 1
11 Other Manufacturing 100 % 0 % 11
12 Other Non-Manufacturing -- -- 3
13 Retail & Wholesale 100 % 0 % 12
14 Services (Non-Financial) 88 % 12 % 24
15 Transportation Equipment -- -- 2
16 <NA> -- -- 0
T4
Industry percentage_Yes percentage_No Total_responses
1 ALL 96 % 4 % 121
2 Banking/Financial Services 86 % 14 % 7
3 Chemicals 100 % 0 % 5
4 Consumer Goods 100 % 0 % 8
5 Energy 100 % 0 % 9
6 High Tech 100 % 0 % 9
7 Insurance/Reinsurance 93 % 7 % 15
8 Life Sciences 91 % 9 % 11
9 Logistics -- -- 3
10 Mining & Metals -- -- 1
11 Other Manufacturing 100 % 0 % 12
12 Other Non-Manufacturing -- -- 3
13 Retail & Wholesale 100 % 0 % 12
14 Services (Non-Financial) 92 % 8 % 24
15 Transportation Equipment -- -- 2
16 <NA> -- -- 0
> dput(T2)
structure(list(Industry = c("ALL", "Banking/Financial Services",
"Chemicals", "Consumer Goods", "Energy", "High Tech", "Insurance/Reinsurance",
"Life Sciences", "Logistics", "Mining & Metals", "Other Manufacturing",
"Other Non-Manufacturing", "Retail & Wholesale", "Services (Non-Financial)",
"Transportation Equipment", NA), percentage_Yes = c("94 %", "83 %",
"100 %", "75 %", "89 %", "100 %", "100 %", "100 %", "--", "--",
"100 %", "--", "100 %", "88 %", "--", "--"), percentage_No = c("6 %",
"17 %", "0 %", "25 %", "11 %", "0 %", "0 %", "0 %", "--", "--",
"0 %", "--", "0 %", "12 %", "--", "--"), Total_responses = c(117,
6, 5, 8, 9, 8, 14, 11, 3, 1, 11, 3, 12, 24, 2, 0)), class = "data.frame", row.names = c(NA,
-16L), .Names = c("Industry", "percentage_Yes", "percentage_No",
"Total_responses"))
> dput(T4)
structure(list(Industry = c("ALL", "Banking/Financial Services",
"Chemicals", "Consumer Goods", "Energy", "High Tech", "Insurance/Reinsurance",
"Life Sciences", "Logistics", "Mining & Metals", "Other Manufacturing",
"Other Non-Manufacturing", "Retail & Wholesale", "Services (Non-Financial)",
"Transportation Equipment", NA), percentage_Yes = c("96 %", "86 %",
"100 %", "100 %", "100 %", "100 %", "93 %", "91 %", "--", "--",
"100 %", "--", "100 %", "92 %", "--", "--"), percentage_No = c("4 %",
"14 %", "0 %", "0 %", "0 %", "0 %", "7 %", "9 %", "--", "--",
"0 %", "--", "0 %", "8 %", "--", "--"), Total_responses = c(121,
7, 5, 8, 9, 9, 15, 11, 3, 1, 12, 3, 12, 24, 2, 0)), class = "data.frame", row.names = c(NA,
-16L), .Names = c("Industry", "percentage_Yes", "percentage_No",
"Total_responses"))
我尝试了表格,但后来又获得了Industry专栏2次:
library("tables")
st<-rbind(data.frame(T2, Employee_Level = 'Exe', what = factor(rownames(T2), levels = rownames(T2)),
row.names= NULL, check.names = FALSE),
data.frame(T4,Employee_Level = 'Mgmt',what = factor(rownames(T4), levels = rownames(T4)),
row.names = NULL,check.names = FALSE))
mytable <- tabular(Heading()*what ~ Employee_Level*(`Industry`+`percentage_Yes`+`percentage_No`+`Total_responses`)*Heading()*(identity),data=st)
latex(mytable)
答案 0 :(得分:0)
这是使用(my)huxtable包的一种方式:
library(huxtable)
my_data <- cbind(T2, T4)[, c(1:4, 6:8)]
my_hux <- as_hux(my_data, add_colnames = TRUE)
my_hux <- insert_row(my_hux, rep("", 7))
my_hux[1, 2] <- "Executive"
my_hux[1, 5] <- "Management"
colspan(my_hux)[1, 2] <- 3
colspan(my_hux)[1, 5] <- 3
my_hux[2, 2:7] <- rep(c("% yes", "% no", "Total responses"), 2)
number_format(my_hux) <- 0
# This should look like what you want:
my_hux