Bug 15820 - option for simplify2array to work with irregular length vectors
Summary: option for simplify2array to work with irregular length vectors
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Wishlist (show other bugs)
Version: R 3.1.0
Hardware: All All
: P5 enhancement
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2014-06-07 03:35 UTC by Joshua N Pritikin
Modified: 2014-06-07 03:35 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joshua N Pritikin 2014-06-07 03:35:45 UTC
It would be nice if there was an option to simplify2array to get this behavior,

mySimplify <- function(par) {
  len <- sapply(par, length)
  biggest <- which(len == max(len))[1]
  out <- matrix(NA, nrow=max(len), ncol=length(par))
  for (x in 1:length(par)) {
    out[1:len[x],x] <- par[[x]]
  }
  rownames(out) <- names(par[[biggest]])
  out
}

Here is an example,

foo <- list(structure(c(1.19178819648695, -0.174546928731224, -0.591428470216157,  -1.09729850119228, -1.33402726089608), .Names = c("a", "b1",  "b2", "b3", "b4")), structure(c(2.76779729827553, 0.78500640067928,  0.77400521202148, 0.763246080345894, 0.294808759565037, -0.326489593374826 ), .Names = c("a", "b1", "b2", "b3", "b4", "b5")), structure(c(0.534631282067101,  0.751391194545935, -1.00950375276966), .Names = c("a", "b1",  "b2")))

> mySimplify(foo)
         [,1]       [,2]       [,3]
a   1.1917882  2.7677973  0.5346313
b1 -0.1745469  0.7850064  0.7513912
b2 -0.5914285  0.7740052 -1.0095038
b3 -1.0972985  0.7632461         NA
b4 -1.3340273  0.2948088         NA
b5         NA -0.3264896         NA