[R] unfold list (variable number of columns) into a data frame

Giovanni Azua bravegag at gmail.com
Sun Oct 23 19:20:43 CEST 2011


Hi Dennis,

Thank you very nice :)

Best regards,
Giovanni

On Oct 23, 2011, at 6:55 PM, Dennis Murphy wrote:

> Hi:
> 
> Here's one approach:
> 
> # Function to process a list component into a data frame
> ff <- function(x) {
>     data.frame(time = x[1], partitioning_mode = x[2], workload = x[3],
>                runtime = as.numeric(x[4:length(x)]) )
>   }
> 
> # Apply it to each element of the list:
> do.call(rbind, lapply(data, ff))
> 
> or equivalently, using the plyr package,
> 
> library('plyr')
> ldply(data, ff)
> 
> # Example:
> L <- list(c("1", "sharding", "query", "607", "85", "52", "79", "77",
> "67", "98"),
>          c("1", "sharding", "refresh", "2932", "2870", "2877", "2868"),
>          c("1", "replication", "query", "2891", "2907", "2922", "2937"))
> do.call(rbind, lapply(L, ff))
>   time partitioning_mode workload runtime
> 1     1          sharding    query     607
> 2     1          sharding    query      85
> 3     1          sharding    query      52
> 4     1          sharding    query      79
> 5     1          sharding    query      77
> 6     1          sharding    query      67
> 7     1          sharding    query      98
> 8     1          sharding  refresh    2932
> 9     1          sharding  refresh    2870
> 10    1          sharding  refresh    2877
> 11    1          sharding  refresh    2868
> 12    1       replication    query    2891
> 13    1       replication    query    2907
> 14    1       replication    query    2922
> 15    1       replication    query    2937
> 
> HTH,
> Dennis



More information about the R-help mailing list