Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple columns, transforming with multiple variables

Tags:

r

How do I add the values from many variables?

If I just had two variables (columns) I could simply go:

summation.variable <- variable1 + variable2

or if it was all in a dataframe:

transform(dataframe, summation.col = column1 + column2)

How do I do it if I have about 10 variables and I do not want to type each one as in col1+col2+col3+col4. To make matters worse my columns have quite long names and at times the exact columns that I use can change. I have a character vector with all the relevant column names in it but cannot think how to use it.

The following is useless since it adds every value in every column in every row and gives a single value for the whole lot.

sum(metrics)
like image 830
Farrel Avatar asked Sep 22 '09 02:09

Farrel


People also ask

Can aggregate function be used for multiple columns?

The Syntax for Using Group By in SQLAny of the aggregate functions can be used on one or more than one of the columns being retrieved.

Which allows you to apply the same summary function to multiple columns at once?

We can use data frames to allow summary functions to return multiple columns.

How do you change the datatype of multiple columns in Excel?

To Format multiple columns you need to go transform/ then highlight set of columns you wnat to change the data format(Percent, Whole number, etc.) then right click and choose Change type. Unfortunately there is no chose from here to select a decimal part but this is something eazy to do from Home tab.


2 Answers

You want to use rowSums (see the indexing with a character vector.)

tmp <- data.frame(a=1:2,b=3:4,d=5:6)
rowSums(tmp[,c("a","d")])

or, more generally, apply:

apply(tmp[,c("a","d")], 1, sum)
like image 117
Eduardo Leoni Avatar answered Sep 27 '22 01:09

Eduardo Leoni


There are many ways to do this kind of operation (ie. apply a function across a row or column), but as Eduardo points out, apply is the most basic:

tmp <- data.frame(a=1:2,b=3:4,d=5:6)
apply(tmp, 1, prod)

This is a very flexible function. For instance, you can do both operations at once with this call:

apply(tmp, MARGIN=1, function(x) c(sum(x), prod(x)))

Performing the same analysis across columns is also simple (the MARGIN parameter describes whether you use rows or columns):

apply(tmp, MARGIN=2, function(x) c(sum(x), prod(x)))
like image 39
Shane Avatar answered Sep 23 '22 01:09

Shane