Better way of adding data.frame columns by referring to indeces
Asked Answered
C

5

1

This question seems basic but I have not been able to find an answer to it.

I want to add columns of a data.frame together by just referring to their indeces.

Suppose I want to add columns 1,2, and 4.

df <- data.frame(
  a=rep(1, 5),
  b=rep(2, 5),
  c=rep(3, 5),
  d=rep(4, 5)
)

I know that explicitly referring to the column names I can do

> df$a + df$b + df$d
[1] 7 7 7 7 7

And referring to indeces I can do

> df[1] + df[2] + df[4]
  a
1 7
2 7
3 7
4 7
5 7

However, the index option above requires me to write out the name of the data.frame for every column I want to add.

Is there a way to add these columns together while just referring to the indeces and the data.frame once?

Coquito answered 26/7, 2016 at 16:4 Comment(0)
W
1

You can use the rowSums function and refer to columns by setting a vector of column numbers in df[, ].

rowSums(df[, c(1,2,4)]
[1] 7 7 7 7 7
Weltschmerz answered 26/7, 2016 at 16:9 Comment(0)
V
2

Or within a data.table:

dt[, sum := rowSums(.SD), .SDcols = c(1, 2, 4)]
dt[, sum := rowSums(.SD), .SDcols = c('a', 'b', 'd')]
Vetavetch answered 26/7, 2016 at 16:14 Comment(0)
W
1

You can use the rowSums function and refer to columns by setting a vector of column numbers in df[, ].

rowSums(df[, c(1,2,4)]
[1] 7 7 7 7 7
Weltschmerz answered 26/7, 2016 at 16:9 Comment(0)
G
1
with(df, a + b + d)
[1] 7 7 7 7 7
Grantgranta answered 26/7, 2016 at 16:17 Comment(1)
Although not appropriate for my current problem this is good to know.Coquito
E
1

Another solution using data.table:

require(data.table)  # Load package
dt <- data.table(df) # Convert to data.table
dt[, a + b + d]      # Sum columns
[1] 7 7 7 7 7  
Experimentalize answered 26/7, 2016 at 16:52 Comment(0)
W
1

Another option is

Reduce(`+`, df[-3])
#[1] 7 7 7 7 7

Or a variant of @PierreLafortune

df$a + df$b + df$d
#[1] 7 7 7 7 7
Whalen answered 1/8, 2016 at 17:55 Comment(4)
I like the Reduce approach because I'm not limited to addition. I can do / and * as well. I'm not sure I understand how this function works though. When I do Reduce(sum, df[-3]) I get 35 but when I do Reduce(max, df[-3]) I get 4.Coquito
@Coquito There are some functions you can use with Reduce and sum will get the whole sum instead of rowwise sum.Whalen
akrun do you know of a function, most likely from a package, that works like Reduce() but is faster? I am using Reduce to merge a list of several hundred dataframes like this Reduce(function(x,y) {full_join(x,y)}, masterlist) . I know this is a little off topic but it might but nice to have an alternative that works like Reduce for arithmetic operations.Coquito
@Coquito I think there is reduce function in one of the hadleyverse packages though not sure.Whalen

© 2022 - 2024 — McMap. All rights reserved.