R使用列表列重新融合data.table
发布时间:2020-12-14 04:34:27 所属栏目:大数据 来源:网络整理
导读:我有一个大的(数百万行)熔化的data.table,通常在变量和值列中展开熔化样式.我需要以宽泛的形式转换表(滚动变量).问题是数据表还有一个名为data的列表列,我需要保留它.这使得无法使用reshape2,因为dcast无法处理非原子列.因此,我需要自己卷起来. 由于列表列,p
我有一个大的(数百万行)熔化的data.table,通常在变量和值列中展开熔化样式.我需要以宽泛的形式转换表(滚动变量).问题是数据表还有一个名为data的列表列,我需要保留它.这使得无法使用reshape2,因为dcast无法处理非原子列.因此,我需要自己卷起来.
由于列表列,previous question关于使用熔化数据表的答案不适用于此处. 我对我提出的解决方案不满意.我正在寻找更简单/更快实现的建议. x <- LETTERS[1:3] dt <- data.table( x=rep(x,each=2),y='d',data=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> 解决方法
我有一些欺骗方法可能会做到这一点 – 重要的是,我认为每个x,y,列表组合都是独一无二的!如果没有,请忽略.
我将创建两个单独的数据表,第一个是没有数据列表对象的数据,第二个只有唯一的数据列表对象和一个键.然后将它们合并在一起以获得所需的结果. require(data.table) require(stringr) require(reshape2) x <- LETTERS[1:3] dt <- data.table( x=rep(x,data=list(list("a","b"),list("c","d")),6) ) # First create the dcasted datatable without the pesky list objects: dt_nolist <- dt[,list(x,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,list dt_list <- dt[,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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |