简体   繁体   English

计算 R 中多列数据的总和、均值和方差

[英]Calculate sum, mean and variance for several columns of data in R

I'm new to R. The professor asked us to obtain sum, mean and variance for several columns of data which are in Excel form.我是R的新手,教授让我们求取Excel表格中几列数据的总和、均值和方差。 Now, I want to try to use R to solve them rather than enter the formula in Excel and drag.现在,我想尝试使用 R 来解决它们,而不是在 Excel 中输入公式并拖动。 I have imported the data into R and they are correctly displayed.我已将数据导入 R,它们已正确显示。 I can use the commands sum () and sd () and var () for EACH column.我可以对每个列使用命令sum ()sd ()以及var ()

My question is: is there a way to let R display the sum, sd, and variance for each column at the same time?我的问题是:有没有办法让R同时显示每一列的总和、sd和方差? (Rather than calculating these again and again for each column). (而不是为每一列一次又一次地计算这些)。

I mean something like colSum(col1, col2, col3,...) and the line just shows the sum for each column.我的意思是类似colSum(col1, col2, col3,...)并且该行仅显示每列的总和。

More generally you would do something like:更一般地说,你会做这样的事情:

sapply(data, sum)
sapply(data, var)
sapply(data, sd)

Or in one line as suggested by Agile Bean:或者按照 Agile Bean 的建议在一行中:

sapply(data, function(x) c(sum=sum(x), var=var(x), sd=sd(x)))

I just figured it out.我刚刚弄明白了。 Basically I need to use colSums() and colMeans().基本上我需要使用 colSums() 和 colMeans()。 For example, colSums (,data[2:5]).例如,colSums (,data[2:5])。 This means we can calculate the sum for each column from column 2 to column 5.这意味着我们可以计算从第 2 列到第 5 列的每一列的总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM