Try this:
> unname(t(do.call(cbind, lapply(a, ts
[,1] [,2] [,3] [,4]
[1,] "a" "b" "c" NA
[2,] "d" "e" NA NA
[3,] "f" "g" "h" "i"
On Mon, Jun 22, 2009 at 7:15 PM, Kenneth Takagi wrote:
> Hi,
>
>
>
> I have a list made up of character strings with each item a different
> length
: Monday, June 22, 2009 7:24 PM
To: Kenneth Takagi
Cc: r-help@r-project.org
Subject: Re: [R] Convert "ragged" list to matrix
try this:
> a
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "d" "e"
[[3]]
[1] "f" "g" "h" &q
Try this:
matrix(unlist(lapply(a, '[', 1:max(sapply(a, length, ncol = 4, byrow =
TRUE)
or
do.call(rbind, lapply(a, '[', 1:max(sapply(a, length
On Mon, Jun 22, 2009 at 8:15 PM, Kenneth Takagi wrote:
> Hi,
>
>
>
> I have a list made up of character strings with each item a different
>
try this:
> a
[[1]]
[1] "a" "b" "c"
[[2]]
[1] "d" "e"
[[3]]
[1] "f" "g" "h" "i"
> # find max row length
> rowMax <- max(sapply(a, length))
> # now create output matrix by lengthening rows
> do.call(rbind, lapply(a, function(x){
+ length(x) <- rowMax
+ x
+ }))
[,1] [,2] [,3] [,4]
[1,]
Hi,
I have a list made up of character strings with each item a different
length (each item is between 1and 6 strings long). Is there a way to
convert a "ragged" list to a matrix such that each item is its own row?
Here is a simple example:
a=list();
a[[1]] = c("a", "b", "c");
a[[2]] = c
5 matches
Mail list logo