gpt4 book ai didi

r - 计算每个数字列的平均值并将结果添加为行

转载 作者:行者123 更新时间:2023-12-04 09:37:49 26 4
gpt4 key购买 nike

所以,我想计算每个数字列的平均值,并将结果放在列下方的行中。让我们从一个数据开始:

> head(tbl_mut)

timetE4_1 timetE1_2 timetE2_2 timetE3_2 timetE4_2 eve_mean mor_mean tot_mean
1 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99
2 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14
3 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92
这就是我想要实现的目标:
timetE4_1  timetE1_2  timetE2_2  timetE3_2  timetE4_2   eve_mean   mor_mean  tot_mean
1 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99
2 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14
3 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92
.....
445 X X X X X X X X
X - 列中值的平均值。
请注意,数据可能包含其他非数字列。

最佳答案

使用 rbindcolMeans 如下:

> rbind(tbl_mut, colMeans = colMeans(tbl_mut))
timetE4_1 timetE1_2 timetE2_2 timetE3_2 timetE4_2 eve_mean mor_mean tot_mean
1 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99
2 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14
3 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92
colMeans 173482.724 497479.54 319083.15 330634.05 331434.59 160144.458 369657.83 264901.15
编辑
假设您的数据框同时包含数字和非数字列(如“描述”列):
> df
Description timetE4_1 timetE1_2 timetE2_2 timetE3_2 timetE4_2 eve_mean mor_mean tot_mean
1 A 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99
2 B 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14
3 C 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4 D 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92
...然后您可以使用 sapply(df, is.numeric) 获取数字列,然后在其上计算 colmeans
> suppressWarnings(rbind(df, colMeans = colMeans(df[, sapply(df, is.numeric)])))
Description timetE4_1 timetE1_2 timetE2_2 timetE3_2 timetE4_2 eve_mean mor_mean tot_mean
1 A 4048.605 59094.48 27675.59 26374.06 43310.01 7774.442 39113.53 23443.99
2 B 45729.986 139889.21 111309.64 129781.17 96924.62 43374.117 119476.16 81425.14
3 C 639686.154 1764684.16 1117027.29 1147967.45 1156442.48 585562.724 1296530.34 941046.53
4 D 4466.153 26250.32 20320.08 18413.54 29061.25 3866.547 23511.30 13688.92
colMeans <NA> 497479.542 319083.15 330634.05 331434.59 160144.46 369657.833 264901.15 173482.72
或者,如果您知道非数字变量的索引,例如第一列,您可以使用 df[, -1] 取消选择该列:
suppressWarnings(rbind(df, colMeans = colMeans(df[, -1]))) 

关于r - 计算每个数字列的平均值并将结果添加为行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19787909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com