I really have a big problem and loop through data.table to do what I want too slow, so I'm trying to get around the loop. Suppose I have data.table as follows:
a <- data.table(i = c(1,2,3), j = c(2,2,6), k = list(c("a","b"),c("a","c"),c("b"))) > a ijk 1: 1 2 a,b 2: 2 2 a,c 3: 3 6 b
And I want to group based on values โโin k. So something like this:
a[, sum(j), by = k]
right now i am getting the following error:
Error in `[.data.table`(a, , sum(i), by = k) : The items in the 'by' or 'keyby' list are length (2,2,1). Each must be same length as rows in x or number of rows returned by i (3).
The answer I'm looking for is to first group all the rows having โaโ in column k and calculate the sum (j), and then all the rows having โbโ, etc. So the desired answer is:
k V1 a 4 b 8 c 2
Any hint on how to do this efficiently? I cannot melt column K by repeating rows, since the size of data.table will be too large for my case.
source share