Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table operations by column name

Tags:

r

data.table

Suppose I have a data.table

a <- data.table(id=c(1,1,2,2,3),a=21:25,b=11:15,key="id")

I can add new columns like this:

a[, sa := sum(a), by="id"]
a[, sb := sum(b), by="id"]
> a
   id  a  b sa sb
1:  1 21 11 43 23
2:  1 22 12 43 23
3:  2 23 13 47 27
4:  2 24 14 47 27
5:  3 25 15 25 15

However, suppose that I have column names instead:

for (n in c("a","b")) {
  s <- paste0("s",n)
  a[, s := sum(n), by="id", with=FALSE] # ERROR: invalid 'type' (character) of argument
}

what do I do?

like image 299
sds Avatar asked Jan 09 '14 15:01

sds


People also ask

How can you identify a column in a Datatable?

By using the Column name or Column index we can identify a column in a data table.

What is a column selector?

Column Selector is a feature that allows user to select at runtime which columns will be visible, which ones to hide and the order in which will be shown in the grid. It must be specified for every grid that we want to have this functionality.

What is a column name?

In the context of a function, a GROUP BY clause, an ORDER BY clause, an expression, or a search condition, a column name refers to values of a column in some target table or view in a DELETE or UPDATE statement or table-reference in a FROM clause.


1 Answers

You can also do this:

a <- data.table(id=c(1,1,2,2,3),a=21:25,b=11:15,key="id")

a[, c("sa", "sb") := lapply(.SD, sum), by = id]

Or slightly more generally:

cols.to.sum = c("a", "b")
a[, paste0("s", cols.to.sum) := lapply(.SD, sum), by = id, .SDcols = cols.to.sum]
like image 118
eddi Avatar answered Sep 26 '22 03:09

eddi