简体   繁体   中英

Sum of every n-th column of a data frame

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, ie 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, ie 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, eg 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?

Here is a more generic approach which however, assumes that the number of columns in your data frame is even number, ie

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, ie 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 ).

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

Edit

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM