Berthrand Eros
Berthrand Eros

Reputation: 105

R: subtract variable amount of columns from target column

I wish to make a function that takes in three arguments: a data.table, a target column and a vector of columns to subtract from the target column:

test = data.table(
  total = c(10,6,4,4,5,6), 
  b = c(9,5,3,3,4,5), 
  c = rep(1, 6),
  d = rep(2, 6)
)

function(data, target = "total", cols = c("b", "c")) should give desired output:

test = data.table(
  total = c(10,6,4,4,5,6), 
  b = c(9,5,3,3,4,5), 
  c = rep(1, 6),
  d = rep(2, 6),
  new_total = rep(0, 6)
)

Attempts so far:

1)

testfunction <- function(dt, target, cols){
    dt[, new_total := target - .SD , .SDcols = cols]

}

2)

testfunction <- function(dt, target, cols){
     temp = copy(dt[, .SD, .SDcols = cols])
     dt[, new_total := target - temp]

 }

None of these work, and I think my attempt show a lack of understanding of data.table, unfortunately.

Upvotes: 0

Views: 91

Answers (1)

s_baldur
s_baldur

Reputation: 33488

Try this:

foo <- function(data, target = "total", cols = c("b", "c")) {
  data[, new_total := get(target) - rowSums(.SD), .SDcols = cols]
}

foo(test)[]

#    total b c d new_total
# 1:    10 9 1 2         0
# 2:     6 5 1 2         0
# 3:     4 3 1 2         0
# 4:     4 3 1 2         0
# 5:     5 4 1 2         0
# 6:     6 5 1 2         0

The problems were

  1. .SD is a data.table while target is just a vector.

  2. to get a column with a string variable can't be done like this, you can, for example, use get(). More examples here.

Upvotes: 2

Related Questions