2

I have several columns (literally) named A1, A2, A3, ... A50 in a data.table in R. Unfortunately, columns in my table are not arranged alphabetically.

I want to create a new column named sumA, which will contain A1 + A2 + ... + A50.

What's a simple (and not tedious) way of doing this?

wwl
  • 2,025
  • 2
  • 30
  • 51

1 Answers1

5

Here is one option with Reduce and +

library(data.table)
dt[, sumA := Reduce("+", .SD)]

If there are other columns i.e. columns other than 'A1:A50' in the dataset, use the .SDcols to specify the columns to select

dt[, sumA := Reduce("+", .SD), .SDcols = paste0("A", 1:50)]

Or as @Arun mentioned, if the columns are ordered, then : can be used to select the column

dt[, sumA := Reduce("+", .SD), .SDcols = A1:A50]
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    For interactive use, you can also specify `startcol:endcol`.. i.e., `.SDcols=A1:A50` (assuming they're the start and end cols). – Arun Aug 30 '16 at 16:20
  • 2
    Thanks! That worked perfect. If any newbies are wondering what Reduce does in R, look here: http://stackoverflow.com/questions/28545688/i-dont-understand-the-function-reduce-in-r – wwl Aug 30 '16 at 16:26
  • 3
    @wwl One thing to watch out for with this approach is the treatment of missing values. The `sum` function has an `na.rm` option, while `+` does not (so you can't "ignore" `NA`s). The linked question shows a way of addressing that. – Frank Aug 30 '16 at 16:55