Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

excel

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. Now, I want to try to use R to solve them rather than enter the formula in Excel and drag. I have imported the data into R and they are correctly displayed. I can use the commands sum () and sd () and var () for EACH column.

My question is: is there a way to let R display the sum, sd, and variance for each column at the same time? (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.

like image 221
pythh Avatar asked Nov 29 '25 11:11

pythh


2 Answers

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:

sapply(data, function(x) c(sum=sum(x), var=var(x), sd=sd(x)))
like image 186
sindri_baldur Avatar answered Dec 02 '25 00:12

sindri_baldur


I just figured it out. Basically I need to use colSums() and colMeans(). For example, colSums (,data[2:5]). This means we can calculate the sum for each column from column 2 to column 5.

like image 36
pythh Avatar answered Dec 01 '25 23:12

pythh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!