发布于 2022-01-24 12:40:40
我在R中找到了一种正确获得枢轴的方法,我使用了data.frame "bhmtrains“的库(”透视“)。现在起作用了。谢谢你的想法。
library(pivottabler)
qhpvt(bhmtrains, c("=","TOC"), "TrainCategory",
c("Mean Speed"="mean(SchedSpeedMPH, na.rm=TRUE)", "Std Dev Speed"="sd(SchedSpeedMPH, na.rm=TRUE)"),
formats=list("%.0f", "%.1f"), totals=list("", "TrainCategory"="All Categories"))发布于 2022-01-22 15:17:57
如果你给我们一个更多的可复制示例,给你一个更适用的答案会更容易。
但是,我认为您想要的是通过dplyr包中的函数很容易地完成。此答案使用内置的iris数据集。它根据分类变量Species进行分组,并按种类计算数据帧中所有数字列的中位数。
library(dplyr)
iris %>%
group_by(Species) %>%
summarise(across(where(is.numeric), median))
# A tibble: 3 x 5
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
<fct> <dbl> <dbl> <dbl> <dbl>
1 setosa 5 3.4 1.5 0.2
2 versicolor 5.9 2.8 4.35 1.3
3 virginica 6.5 3 5.55 2 若要计算所有数值列的平均值,请在上面的代码有mean的地方使用median。
https://stackoverflow.com/questions/70814109
复制相似问题