Reputation: 189
I was wondering why this code does not simply concatenate the two data frames.
library(data.table)
dt <- data.table(a=c(1,2))
df <- vector("list", 2)
df[[1]] <- as.list(data.frame(b=c('a','b','c', 'd'),
c=c(11, 22, 33, 99),
z=c('aa','bb','cc', 'dd')))
df[[2]] <- as.list(data.frame(b=c('b','e', 'f', 'g'),
c=c(44,55,66,77),
z=c('gg','bb','hh', 'dd')))
getData <- function(i) {
return(df[[i]])
}
dt <- dt[, getData(a), by=a]
print(dt)
I was expecting a result as follows:
a b c z
1: 1 a 11 aa
2: 1 b 22 bb
3: 1 c 33 cc
4: 1 d 99 dd
5: 2 b 44 gg
6: 2 e 55 bb
7: 2 f 66 hh
8: 2 g 77 dd
But I get the following:
a b c z
1: 1 a 11 aa
2: 1 b 22 bb
3: 1 c 33 cc
4: 1 d 99 dd
5: 2 a 44 cc
6: 2 b 55 aa
7: 2 c 66 dd
8: 2 d 77 bb
Upvotes: 2
Views: 61
Reputation: 1196
If you add the definition of df$a
like this:
df[[1]] <- as.list(data.frame(a=1,
b=c('a','b','c', 'd'),
c=c(11, 22, 33, 99),
z=c('aa','bb','cc', 'dd')))
df[[2]] <- as.list(data.frame(a=2,
b=c('b','e', 'f', 'g'),
c=c(44,55,66,77),
z=c('gg','bb','hh', 'dd')))
I believe all you need to do to accomplish this is use rbindlist()
, like so:
> result <- rbindlist(df)
> result
a b c z
1: 1 a 11 aa
2: 1 b 22 bb
3: 1 c 33 cc
4: 1 d 99 dd
5: 2 b 44 gg
6: 2 e 55 bb
7: 2 f 66 hh
8: 2 g 77 dd
Upvotes: 2