Reputation: 61
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.
Upvotes: 3
Views: 27075
Reputation: 33488
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)))
Upvotes: 11
Reputation: 61
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.
Upvotes: 3