从数据框创建汇总统计表

编程入门 行业动态 更新时间:2024-10-09 16:29:33
本文介绍了从数据框创建汇总统计表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下5个变量的29个观察结果(df):

I have the following data frame (df) of 29 observations of 5 variables:

age height_seca1 height_chad1 height_DL weight_alog1 1 19 1800 1797 180 70 2 19 1682 1670 167 69 3 21 1765 1765 178 80 4 21 1829 1833 181 74 5 21 1706 1705 170 103 6 18 1607 1606 160 76 7 19 1578 1576 156 50 8 19 1577 1575 156 61 9 21 1666 1665 166 52 10 17 1710 1716 172 65 11 28 1616 1619 161 66 12 22 1648 1644 165 58 13 19 1569 1570 155 55 14 19 1779 1777 177 55 15 18 1773 1772 179 70 16 18 1816 1809 181 81 17 19 1766 1765 178 77 18 19 1745 1741 174 76 19 18 1716 1714 170 71 20 21 1785 1783 179 64 21 19 1850 1854 185 71 22 31 1875 1880 188 95 23 26 1877 1877 186 106 24 19 1836 1837 185 100 25 18 1825 1823 182 85 26 19 1755 1754 174 79 27 26 1658 1658 165 69 28 20 1816 1818 183 84 29 18 1755 1755 175 67

我希望获得平均值,标准每个变量的偏差,中值,最小值,最大值和样本大小,并获得输出作为数据帧。我尝试使用下面的代码,但是我不可能使用和使用自定义或聚合似乎超出了我作为一个新手R程序员。我的任务要求我不要使用任何'额外'R包。

I wish to obtain the mean, standard deviation, median, minimum, maximum and sample size of each of the variables and get an output as a data frame. I tried using the code below but then the it becomes impossible for me to work with and using tapply or aggregate seems to be beyond me as a novice R programmer. My assignment requires me not use any 'extra' R packages.

apply(df, 2, mean) apply(df, 2, sd) apply(df, 2, median) apply(df, 2, min) apply(df, 2, max) apply(df, 2, length)

理想情况下,输出数据框应如此显示,包括每个统计功能的行标题:

Ideally, this is how the output data frame should look like including the row headings for each of the statistical functions:

age height_seca1 height_chad1 height_DL weight_alog1 mean 20 1737 1736 173 73 sd 3.3 91.9 92.7 9.7 14.5 median 19 1755 1755 175 71 minimum 17 1569 1570 155 50 maximum 31 1877 1880 188 106 sample size 29 29 29 29 29

任何帮助将不胜感激。

Any help would be greatly appreciated.

推荐答案

或使用您已经完成的任务,您只需将这些摘要放入列表中,然后使用 do.call

Or using what you have already done, you just need to put those summaries into a list and use do.call

df <- psych::read.clipboard() tmp <- do.call(data.frame, list(mean = apply(df, 2, mean), sd = apply(df, 2, sd), median = apply(df, 2, median), min = apply(df, 2, min), max = apply(df, 2, max), n = apply(df, 2, length))) tmp mean sd median min max n age 20.41379 3.300619 19 17 31 29 height_seca1 1737.24138 91.919474 1755 1569 1877 29 height_chad1 1736.48276 92.682492 1755 1570 1880 29 height_DL 173.37931 9.685828 175 155 188 29 weight_alog1 73.41379 14.541854 71 50 106 29

或...

data.frame(t(tmp)) age height_seca1 height_chad1 height_DL weight_alog1 mean 20.413793 1737.24138 1736.48276 173.379310 73.41379 sd 3.300619 91.91947 92.68249 9.685828 14.54185 median 19.000000 1755.00000 1755.00000 175.000000 71.00000 min 17.000000 1569.00000 1570.00000 155.000000 50.00000 max 31.000000 1877.00000 1880.00000 188.000000 106.00000 n 29.000000 29.00000 29.00000 29.000000 29.00000

更多推荐

从数据框创建汇总统计表

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

发布评论

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

>www.elefans.com

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