Re: [R] Convert a list to matrix

2009-02-22 Thread Dimitris Rizopoulos
one way is the following: m <- list(A = 1, B = 1:2, C = 1:3, D = 1:4) n <- max(sapply(m, length)) t(sapply(m, function (x) c(x, rep(NA, n - length(x) I hope it helps. Best, Dimitris Daren Tan wrote: I would like to convert a list to matrix. This can be easily achieved via do.call. The

Re: [R] Convert a list to matrix

2009-02-21 Thread jim holtman
This should do what you want: > m <- list() > m[["A"]] <- 1 > m[["B"]] <- 2:3 > # get the maximum length > maxLen <- max(sapply(m, length)) > # create a new list with elements padded out with NAs > newM <- lapply(m, function(.ele){ + c(.ele, rep(NA, maxLen))[1:maxLen] + }) > do.call(rbind, new

[R] Convert a list to matrix

2009-02-21 Thread Daren Tan
I would like to convert a list to matrix. This can be easily achieved via do.call. The only problem is each element of the list has different length, which causes the recycling of values. How can I have NA instead of recycled values ? m <- list() m[["A"]] <- 1 m[["B"]] <- 2:3 do.call(rbind, m) [