向 table 添加行进行故障排除
Troubleshooting adding a row to a table
我正在尝试制作一个 table 来查看调查问题的不同答案的百分比响应。答案是 1(最差)到 5(最好),我已经成功地做出了结果 table:Table 1
这是 Table 1
的 dput()
dput(t1)
structure(c(1.33196721311475, 0.867678958785249, 0.995024875621891,
0.544069640914037, 1.0498687664042, 0, 0.759219088937093, 0.66334991708126,
0.217627856365615, 0, 1.63934426229508, 6.83297180043384, 0.995024875621891,
0.870511425462459, 1.0498687664042, 19.5696721311475, 37.0932754880694,
26.6998341625207, 20.2393906420022, 20.4724409448819, 77.4590163934426,
54.4468546637744, 70.6467661691542, 78.1284004352557, 77.4278215223097
), .Dim = c(5L, 5L), .Dimnames = structure(list(c("A", "B", "C",
"D", "E"), c("1", "2", "3", "4", "5")), .Names = c("", "")))
生成者:
t1<- t(apply(table(cleandata$`Event Name`, cleandata$Q1), 1, function(x) (x/sum(x)*100)))
如何使用列均值在 table 的底部添加一行?我已经尝试将 add_row 与 tidyverse、rbind 和 bind_rows 一起使用,但似乎没有任何效果。我得到的是 colMean(t1) 的列平均值,但也许我用错了方法?
使用 rbind
你可以:
rbind(t1, Mean = colMeans(t1))
#> 1 2 3 4 5
#> A 1.3319672 0.0000000 1.6393443 19.56967 77.45902
#> B 0.8676790 0.7592191 6.8329718 37.09328 54.44685
#> C 0.9950249 0.6633499 0.9950249 26.69983 70.64677
#> D 0.5440696 0.2176279 0.8705114 20.23939 78.12840
#> E 1.0498688 0.0000000 1.0498688 20.47244 77.42782
#> Mean 0.9577219 0.3280394 2.2775442 24.81492 71.62177
由 reprex package (v2.0.1)
于 2022-05-19 创建
我更喜欢@stefan 的解决方案,但是如果你想使用 add_row
,你需要将它从矩阵转换为数据框,例如
library(dplyr)
tab <- cbind(names = rownames(t1), as_tibble(t1)) %>%
add_row(names = "Mean", t1 %>%
as_tibble() %>%
summarise(across(everything(), mean)))
colnames(tab)[1] <- ""
输出:
1 2 3 4 5
1 A 1.3319672 0.0000000 1.6393443 19.56967 77.45902
2 B 0.8676790 0.7592191 6.8329718 37.09328 54.44685
3 C 0.9950249 0.6633499 0.9950249 26.69983 70.64677
4 D 0.5440696 0.2176279 0.8705114 20.23939 78.12840
5 E 1.0498688 0.0000000 1.0498688 20.47244 77.42782
6 Mean 0.9577219 0.3280394 2.2775442 24.81492 71.62177
我正在尝试制作一个 table 来查看调查问题的不同答案的百分比响应。答案是 1(最差)到 5(最好),我已经成功地做出了结果 table:Table 1
这是 Table 1
的 dput()dput(t1)
structure(c(1.33196721311475, 0.867678958785249, 0.995024875621891,
0.544069640914037, 1.0498687664042, 0, 0.759219088937093, 0.66334991708126,
0.217627856365615, 0, 1.63934426229508, 6.83297180043384, 0.995024875621891,
0.870511425462459, 1.0498687664042, 19.5696721311475, 37.0932754880694,
26.6998341625207, 20.2393906420022, 20.4724409448819, 77.4590163934426,
54.4468546637744, 70.6467661691542, 78.1284004352557, 77.4278215223097
), .Dim = c(5L, 5L), .Dimnames = structure(list(c("A", "B", "C",
"D", "E"), c("1", "2", "3", "4", "5")), .Names = c("", "")))
生成者:
t1<- t(apply(table(cleandata$`Event Name`, cleandata$Q1), 1, function(x) (x/sum(x)*100)))
如何使用列均值在 table 的底部添加一行?我已经尝试将 add_row 与 tidyverse、rbind 和 bind_rows 一起使用,但似乎没有任何效果。我得到的是 colMean(t1) 的列平均值,但也许我用错了方法?
使用 rbind
你可以:
rbind(t1, Mean = colMeans(t1))
#> 1 2 3 4 5
#> A 1.3319672 0.0000000 1.6393443 19.56967 77.45902
#> B 0.8676790 0.7592191 6.8329718 37.09328 54.44685
#> C 0.9950249 0.6633499 0.9950249 26.69983 70.64677
#> D 0.5440696 0.2176279 0.8705114 20.23939 78.12840
#> E 1.0498688 0.0000000 1.0498688 20.47244 77.42782
#> Mean 0.9577219 0.3280394 2.2775442 24.81492 71.62177
由 reprex package (v2.0.1)
于 2022-05-19 创建我更喜欢@stefan 的解决方案,但是如果你想使用 add_row
,你需要将它从矩阵转换为数据框,例如
library(dplyr)
tab <- cbind(names = rownames(t1), as_tibble(t1)) %>%
add_row(names = "Mean", t1 %>%
as_tibble() %>%
summarise(across(everything(), mean)))
colnames(tab)[1] <- ""
输出:
1 2 3 4 5
1 A 1.3319672 0.0000000 1.6393443 19.56967 77.45902
2 B 0.8676790 0.7592191 6.8329718 37.09328 54.44685
3 C 0.9950249 0.6633499 0.9950249 26.69983 70.64677
4 D 0.5440696 0.2176279 0.8705114 20.23939 78.12840
5 E 1.0498688 0.0000000 1.0498688 20.47244 77.42782
6 Mean 0.9577219 0.3280394 2.2775442 24.81492 71.62177