Try this:

sapply(dat, '[', 1:max(sapply(dat, length)))


On Fri, Sep 3, 2010 at 10:31 AM, Ron Michael <ron_michae...@yahoo.com>wrote:

> Thanks Henrique and Marc for your reply, both worked.
>
> However I need one more suggestion on from list to matrix
>
> dat <- vector("list", 3)
> dat[[1]] <- rnorm(5)
> dat[[2]] <- rnorm(15)
> dat[[3]] <- rnorm(3)
> > dat
> [[1]]
> [1] 0.7615421 0.4151374 0.6534402 1.5999338 1.4131837
> [[2]]
>  [1]  0.7364765  2.0786904 -1.1087768  1.5091391 -0.1448301  1.6015788
> 1.8039138  1.1194863  1.4907951  0.5324749
> [11]  0.6792318 -0.8328359 -1.8009706 -0.8523432  0.5314461
> [[3]]
> [1] -0.4350899 -0.2610924 -0.6489858
> Now I want to convert "dat" to a matrix. I tried following:
>
> > Reduce("cbind", dat)
>            init
>  [1,] 0.7615421  0.7364765 -0.4350899
>  [2,] 0.4151374  2.0786904 -0.2610924
>  [3,] 0.6534402 -1.1087768 -0.6489858
>  [4,] 1.5999338  1.5091391 -0.4350899
>  [5,] 1.4131837 -0.1448301 -0.2610924
>  [6,] 0.7615421  1.6015788 -0.6489858
>  [7,] 0.4151374  1.8039138 -0.4350899
>  [8,] 0.6534402  1.1194863 -0.2610924
>  [9,] 1.5999338  1.4907951 -0.6489858
> [10,] 1.4131837  0.5324749 -0.4350899
> [11,] 0.7615421  0.6792318 -0.2610924
> [12,] 0.4151374 -0.8328359 -0.6489858
> [13,] 0.6534402 -1.8009706 -0.4350899
> [14,] 1.5999338 -0.8523432 -0.2610924
> [15,] 1.4131837  0.5314461 -0.6489858
>
> Here you see values of vector with smaller length is getting repeated.
> Instead of repetition, I would like to fill those with "NA" (or possibly
> some predefined fixed value)
>
> Would you please suggest me how to do that?
>
> Thanks,
>
>
> --- On *Fri, 3/9/10, Henrique Dallazuanna <www...@gmail.com>* wrote:
>
>
> From: Henrique Dallazuanna <www...@gmail.com>
> Subject: Re: [R] Matrix to list
> To: "Ron Michael" <ron_michae...@yahoo.com>
> Cc: r-h...@stat.math.ethz.ch
> Date: Friday, 3 September, 2010, 8:07 PM
>
>
> Try this:
>
> unclass(as.data.frame(mat))
>
>
> On Fri, Sep 3, 2010 at 9:48 AM, Ron Michael 
> <ron_michae...@yahoo.com<http://aa.mc638.mail.yahoo.com/mc/compose?to=ron_michae...@yahoo.com>
> > wrote:
>
> Dear all, suppose I have following matrix:
>
> > mat <- matrix(rnorm(25), 5)
> >
> > mat
>             [,1]       [,2]        [,3]       [,4]       [,5]
> [1,]  0.97056228 -1.3278509 -0.73511792  2.1650629 -0.4411997
> [2,]  0.58613700 -0.2559899 -1.18334248 -1.4990907  1.8138846
> [3,] -1.03333313  2.0227887  0.89622681  0.6483973 -1.5523283
> [4,]  0.38968833  0.2490004 -0.02301061 -0.2705150 -0.9237268
> [5,]  0.03306289 -0.4022751 -0.44404905 -1.6810542 -0.1016683
>
> Now from "mat" I would like to create a "list" object where i-th column of
> "mat" will represent i-th element of that list object.
>
> I am looking for some way to avoid for loop. It will be really helpful if
> somebody points me on that.
>
> Thanks,
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@r-project.org<http://aa.mc638.mail.yahoo.com/mc/compose?to=r-h...@r-project.org>mailing
>  list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<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
>
>
>


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

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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.

Reply via email to