Tavi
Tavi

Reputation: 2748

get pairwise sums of multiple columns in dataframe

I have a dataframe that looks like this:

x<-data.frame(a=6, b=5:1, c=7, d=10:6)

> x
  a b c  d
1 6 5 7 10
2 6 4 7  9
3 6 3 7  8
4 6 2 7  7
5 6 1 7  6

I am trying to get the sums of columns a & b and c&d in another data frame that should look like:

> new
  ab cd
1 11 17
2 10 16
3  9 15
4  8 14
5  7 13

I've tried the rowSums() function but it returns the sum of ALL the columns per row, and I tried rowSums(x[c(1,2), c(3,4)]) but nothing works. Please help!!

Upvotes: 1

Views: 2105

Answers (2)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193637

Here's another option:

> sapply(split.default(x, 0:(length(x)-1) %/% 2), rowSums)
      0  1
[1,] 11 17
[2,] 10 16
[3,]  9 15
[4,]  8 14
[5,]  7 13

The 0:(length(x)-1) %/% 2 step creates a sequence of groups of 2 that can be used with split. It will also handle odd numbers of columns (treating the final column as a group of its own). Since there's a different default split "method" for data.frames that splits by rows, you need to specify split.default to split the columns into groups.

Upvotes: 0

Rich Scriven
Rich Scriven

Reputation: 99351

You can use rowSums on a column subset.

As a data frame:

data.frame(ab = rowSums(x[c("a", "b")]), cd = rowSums(x[c("c", "d")]))
#   ab cd
# 1 11 17
# 2 10 16
# 3  9 15
# 4  8 14
# 5  7 13 

As a matrix:

cbind(ab = rowSums(x[1:2]), cd = rowSums(x[3:4]))

For a wider data frame, you can also use sapply over a list of column subsets.

sapply(list(1:2, 3:4), function(y) rowSums(x[y]))

For all pairwise column combinations:

y <- combn(ncol(x), 2L, function(y) rowSums(x[y]))
colnames(y) <- combn(names(x), 2L, paste, collapse = "")
y
#      ab ac ad bc bd cd
# [1,] 11 13 16 12 15 17
# [2,] 10 13 15 11 13 16
# [3,]  9 13 14 10 11 15
# [4,]  8 13 13  9  9 14
# [5,]  7 13 12  8  7 13

Upvotes: 1

Related Questions