[datatable-help] "ungrouping" a data.table

Bacou, Melanie mel at mbacou.com
Tue Jan 12 04:22:00 CET 2016


Assuming all list columns are of the same lengths, then this might be a 
little faster?

dtUngroup <- function(dt) {
   l <- names(dt)[dt[, lapply(.SD, class)]=="list"]
   if (length(l)>0) {
     nl <- setdiff(names(dt), l)
     t <- sapply(dt[[l[1]]], length)
     tmp <- dt[, .SD, .SDcols=nl]
     tmp <- tmp[, lapply(.SD, rep, times=t)]
     tmp <- cbind(tmp, dt[, lapply(.SD, unlist), .SDcols=l])
   } else tmp <- dt
   return(tmp)
}

# If you need to cast columns back to POSIXct, might be easier as follows:
res <- dtUngroup(DT)
res[, c := as.POSIXct(c, origin="1970-01-01")]

On 1/8/2016 9:23 AM, statquant3 wrote:
> dtUngroup <- function(DT){
> 	colClasses <- lapply(DT,FUN=class)
> 	listCols <- which(colClasses=='list')
> 	if(length(listCols)>0){
> 		nonListCols <- setdiff(colnames(DT),listCols)
> 		nbListElem <- unlist(DT[,lapply(.SD,FUN=lengths),.SDcols=(listCols[1L])])
> 		DT1 <- DT[,lapply(.SD,FUN=rep,times=(nbListElem)),.SDcols=(nonListCols)]
> 		DT1[,(listCols):=DT[,lapply(.SD,FUN=function(x)
> do.call('c',x)),.SDcols=(listCols)]]
> 		return(DT1)
> 	}
> 	return(DT)
> }
>
> This works... still 20x slower than the equivalent in kdb
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/ungrouping-a-data-table-tp4716265p4716269.html
> Sent from the datatable-help mailing list archive at Nabble.com.
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help



More information about the datatable-help mailing list