Saving a list as a Matrix

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving a list as a Matrix

wizykid
Hi there.

I went through the manual but I couldn't find a solution for my problem.

I have list like this one :
> lst1
[[1]]
[1] 0 1 2 3

[[2]]
[1] 0 1 5

[[3]]
[1] 2 3 4

and I want to save it as Matrix in Matlab mat format like :
0 1 2 3
0 1 5 0
2 3 4 0


can any body help me ? Appreciate your help and thanks in advance.

Reza
Reply | Threaded
Open this post in threaded view
|

Re: Saving a list as a Matrix

Pete Brecknock
wizykid wrote
Hi there.

I went through the manual but I couldn't find a solution for my problem.

I have list like this one :
> lst1
[[1]]
[1] 0 1 2 3

[[2]]
[1] 0 1 5

[[3]]
[1] 2 3 4

and I want to save it as Matrix in Matlab mat format like :
0 1 2 3
0 1 5 0
2 3 4 0


can any body help me ? Appreciate your help and thanks in advance.

Reza
Not pretty but this works ...

lst1 = list(c(0,1,2,3),c(0,1,5),c(2,3,4))

t(sapply(lst1, function(x) c(x,rep(0,4-length(x)))))

HTH

Pete
Reply | Threaded
Open this post in threaded view
|

Re: Saving a list as a Matrix

wizykid
Thank you so much Pete. Have a good one as you made mine
Reply | Threaded
Open this post in threaded view
|

Re: Saving a list as a Matrix

Henrique Dallazuanna
In reply to this post by wizykid
Try this:

t(sapply(lst1, '[', 1:max(sapply(lst1, length))))

On Sat, Sep 3, 2011 at 3:37 PM, wizykid <[hidden email]> wrote:

> Hi there.
>
> I went through the manual but I couldn't find a solution for my problem.
>
> I have list like this one :
>> lst1
> [[1]]
> [1] 0 1 2 3
>
> [[2]]
> [1] 0 1 5
>
> [[3]]
> [1] 2 3 4
>
> and I want to save it as Matrix in Matlab mat format like :
> 0 1 2 3
> 0 1 5 0
> 2 3 4 0
>
>
> can any body help me ? Appreciate your help and thanks in advance.
>
> Reza
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Saving-a-list-as-a-Matrix-tp3788274p3788274.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.