Sim*_*Sim 5 r bigdata reshape data.table
我有一个大的(数百万行)融化data.table
了通常melt
风格的展开variable
和value
列.我需要以宽泛的形式转换表(滚动变量).问题是数据表还有一个名为list的列data
,我需要保留它.这使得它无法使用,reshape2
因为dcast
无法处理非原子列.因此,我需要自己卷起来.
由于列表列,上一个关于使用熔化数据表的问题的答案在这里不适用.
我对我提出的解决方案不满意.我正在寻找更简单/更快实现的建议.
x <- LETTERS[1:3]
dt <- data.table(
x=rep(x, each=2),
y='d',
data=list(list(), list(), list(), list(), list(), list()),
variable=rep(c('var.1', 'var.2'), 3),
value=seq(1,6)
)
# Column template set up
list_template <- Reduce(
function(l, col) { l[[col]] <- col; l },
unique(dt$variable),
list())
# Expression set up
q <- substitute({
l <- lapply(
list_template,
function(col) .SD[variable==as.character(col)]$value)
l$data = .SD[1,]$data
l
}, list(list_template=list_template))
# Roll up
dt[, eval(q), by=list(x, y)]
x y var.1 var.2 data
1: A d 1 2 <list>
2: B d 3 4 <list>
3: C d 5 6 <list>
Run Code Online (Sandbox Code Playgroud)
我有一些作弊方法可能会成功 - 重要的是,我假设每个 x,y,列表组合都是唯一的!如果没有,请忽略。
我将创建两个单独的数据表,第一个数据表没有数据列表对象,第二个数据表仅包含唯一的数据列表对象和一个键。然后将它们合并在一起即可获得所需的结果。
require(data.table)
require(stringr)
require(reshape2)
x <- LETTERS[1:3]
dt <- data.table(
x=rep(x, each=2),
y='d',
data=list(list("a","b"), list("c","d")),
variable=rep(c('var.1', 'var.2'), 3),
value=seq(1,6)
)
# First create the dcasted datatable without the pesky list objects:
dt_nolist <- dt[,list(x,y,variable,value)]
dt_dcast <- data.table(dcast(dt_nolist,x+y~variable,value.var="value")
,key=c("x","y"))
# Second: create a datatable with only unique "groups" of x,y, list
dt_list <- dt[,list(x,y,data)]
# Rows are duplicated so I'd like to use unique() to get rid of them, but
# unique() doesn't work when there's list objects in the data.table.
# Instead so I cheat by applying a value to each row within an x,y "group"
# that is unique within EACH group, but present within EVERY group.
# Then just simply subselect based on that unique value.
# I've chosen rank(), but no doubt there's other options
dt_list <- dt_list[,rank:=rank(str_c(x,y),ties.method="first"),by=str_c(x,y)]
# now keep only one row per x,y "group"
dt_list <- dt_list[rank==1]
setkeyv(dt_list,c("x","y"))
# drop the rank since we no longer need it
dt_list[,rank:=NULL]
# Finally just merge back together
dt_final <- merge(dt_dcast,dt_list)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2215 次 |
最近记录: |