Reputation: 763
Let's assume the data,
a <- c(10, 20, 30, 40, 50)
b <- c(100, 200, 300, 400, 500)
c <- c(1, 2, 3, 4, 5)
d <- c(5, 4, 3, 2, 1)
df <- data.frame(a, b, c, d)
df
a b c d
1 10 100 1 5
2 20 200 2 4
3 30 300 3 3
4 40 400 4 2
5 50 500 5 1
I want to sum every alternate columns, i.e. a+c
and b+d
and so on. The solution should be applicable or modified very easily to other cases like summing every second column, i.e. a+c
, b+d
, c+e
etc. For the example above, the solution should look like this,
> dfsum
aplusc bplusd
1 11 105
2 22 204
3 33 303
4 44 402
5 55 501
Is there any easy way to do that? I have figured out how to do sequential sum, e.g. df[,c(T, F)] + df[,c(F, T)];
, but how to do sum of every n-th column? Besides rbase, is there any tidy solution for this problem?
Upvotes: 4
Views: 95
Reputation: 2299
One approach is to use mutate
:
library(tidyverse)
df %>%
mutate(aplusc = a + c,
bplusd = b + d) %>%
select(aplusc, bplusd)
#aplusc bplusd
#1 11 105
#2 22 204
#3 33 303
#4 44 402
#5 55 501
Here's an approach based on @Sotos's anwer, so it could work on a larger dataset:
Reduce(`+`, split.default(df, (seq_along(df) - 1) %/% 2))
Upvotes: 2
Reputation: 51592
Here is a more generic approach which however, assumes that the number of columns in your data frame is even number, i.e.
n = 2
Reduce(`+`, split.default(df, rep(seq(ncol(df) / n), each = ncol(df) / n)))
# a b
#1 11 105
#2 22 204
#3 33 303
#4 44 402
#5 55 501
The above basically splits the dataframe every 2 columns, i.e. a and b
, c and d
. Using Reduce
, all first elements are added together, then all seconds and so on. So for your case, a
will be added with c
, and b
with d
. If you want to take the sum every 3 columns, just change the denominator of the above split.default
method to 3. However, note that you must have a number of columns divisible by 3 (or by any n
).
Upvotes: 4