如何获得除一栏以外的所有栏的均值?

编程入门 行业动态 更新时间:2024-10-27 15:26:08
本文介绍了如何获得除一栏以外的所有栏的均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有如下数据:

> dplyr::tbl_df(sbp) Country X1980 X1981 X1982 X1983 X1984 X1985 Albania 132.9270 133.0296 133.1459 133.1868 133.2048 133.2577 Algeria 132.4093 132.1710 131.9649 131.7835 131.6161 131.4345 Andorra 140.8585 140.1076 139.3727 138.6457 137.9525 137.3192

我想获取所有国家/地区每年的平均值,并在数据框的末尾添加一个类似World的行,以便我可以以这种格式绘制平均值在多年内的变化.

I want to get mean of values for each year for all countries and add a row like World to the end of the dataframe, so that I can plot the change of the mean value through years, in that format.

我尝试使用gather(),这样我的数据只有三列,例如Country-year-value.但是我想不出一种方法来计算世界平均值.

I tried using gather() so that I have a data with three columns only, like Country-year-value. However I can not think of a way to calculate the mean for the world.

Country year sbp Albania X1980 132.9270 Algeria X1980 132.4093 Andorra X1980 140.8585

可以请教吗?

推荐答案

以R为底的可能解决方案:

A possible solution with base R:

rbind(mydf, cbind(Country = 'World', as.data.frame.list(colMeans(mydf[,-1]))))

给出:

Country X1980 X1981 X1982 X1983 X1984 X1985 1 Albania 132.9270 133.0296 133.1459 133.1868 133.2048 133.2577 2 Algeria 132.4093 132.1710 131.9649 131.7835 131.6161 131.4345 3 Andorra 140.8585 140.1076 139.3727 138.6457 137.9525 137.3192 4 World 135.3983 135.1027 134.8278 134.5387 134.2578 134.0038

和tidyverse解决方案:

mydf %>% gather(year, sbp, -1) %>% bind_rows(., mydf %>% gather(year, sbp, -1) %>% group_by(year) %>% summarise(Country = 'World', sbp = mean(sbp)))

格式较长的结果:

Country year sbp 1 Albania X1980 132.9270 2 Algeria X1980 132.4093 3 Andorra X1980 140.8585 4 Albania X1981 133.0296 5 Algeria X1981 132.1710 6 Andorra X1981 140.1076 7 Albania X1982 133.1459 8 Algeria X1982 131.9649 9 Andorra X1982 139.3727 10 Albania X1983 133.1868 11 Algeria X1983 131.7835 12 Andorra X1983 138.6457 13 Albania X1984 133.2048 14 Algeria X1984 131.6161 15 Andorra X1984 137.9525 16 Albania X1985 133.2577 17 Algeria X1985 131.4345 18 Andorra X1985 137.3192 19 World X1980 135.3983 20 World X1981 135.1027 21 World X1982 134.8278 22 World X1983 134.5387 23 World X1984 134.2578 24 World X1985 134.0038

使用的数据:

Used data:

mydf <- read.table(text="Country X1980 X1981 X1982 X1983 X1984 X1985 Albania 132.9270 133.0296 133.1459 133.1868 133.2048 133.2577 Algeria 132.4093 132.1710 131.9649 131.7835 131.6161 131.4345 Andorra 140.8585 140.1076 139.3727 138.6457 137.9525 137.3192", header=TRUE, stringsAsFactors=FALSE)

更多推荐

如何获得除一栏以外的所有栏的均值?

本文发布于:2023-10-14 19:04:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何获得   均值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!