[R] Question on list object

2011-04-27 Thread Bogaso Christofer
Dear all, let say, I have following list object:

 

listObj - vector(list, length = 3)

listObj[[1]] - rnorm(3)

listObj[[2]] - rnorm(4)

listObj[[3]] - rnorm(5)

 

Now I want to convert above list into a Matrix. Ofcourse I can do it using
Reduce(rbind, listObj). However as you notice that as elements of that
list are arbitrary length vectors, I cant use this trick. What I want is to
have a matrix with 3x5 dimension, where the remaining element of each row
with be filled with NA, i.e I want :

 

rbind(c(listObj[[1]], c(NA, NA)), c(listObj[[2]], c(NA)),listObj[[3]])

 

Is there any better way on how I can do that more directly?

 

Thanks and regards,


[[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.


Re: [R] Question on list object

2011-04-27 Thread Jorge Ivan Velez
Hi Christofer,

You might try

sapply(listObj, function(l) l[1:max(sapply(listObj, length))] )

HTH,
Jorge


On Wed, Apr 27, 2011 at 1:23 PM, Bogaso Christofer  wrote:

 Dear all, let say, I have following list object:



 listObj - vector(list, length = 3)

 listObj[[1]] - rnorm(3)

 listObj[[2]] - rnorm(4)

 listObj[[3]] - rnorm(5)



 Now I want to convert above list into a Matrix. Ofcourse I can do it using
 Reduce(rbind, listObj). However as you notice that as elements of that
 list are arbitrary length vectors, I cant use this trick. What I want is to
 have a matrix with 3x5 dimension, where the remaining element of each row
 with be filled with NA, i.e I want :



 rbind(c(listObj[[1]], c(NA, NA)), c(listObj[[2]], c(NA)),listObj[[3]])



 Is there any better way on how I can do that more directly?



 Thanks and regards,


[[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.


[[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.


Re: [R] Question on list object

2011-04-27 Thread Phil Spector

Here's one way:


uselen = max(sapply(listObj,length))
do.call(rbind,lapply(listObj,function(x){length(x) = uselen;x}))

   [,1]  [,2]   [,3]   [,4]  [,5]
[1,]  0.0702225 -1.143031  1.6437560 NANA
[2,] -0.6100869  2.657910 -0.6028418 -0.7739858NA
[3,] -2.5787357  1.381395 -1.6545857  0.8239982 -1.169961

- Phil Spector
 Statistical Computing Facility
 Department of Statistics
 UC Berkeley
 spec...@stat.berkeley.edu



On Wed, 27 Apr 2011, Bogaso Christofer wrote:


Dear all, let say, I have following list object:



listObj - vector(list, length = 3)

listObj[[1]] - rnorm(3)

listObj[[2]] - rnorm(4)

listObj[[3]] - rnorm(5)



Now I want to convert above list into a Matrix. Ofcourse I can do it using
Reduce(rbind, listObj). However as you notice that as elements of that
list are arbitrary length vectors, I cant use this trick. What I want is to
have a matrix with 3x5 dimension, where the remaining element of each row
with be filled with NA, i.e I want :



rbind(c(listObj[[1]], c(NA, NA)), c(listObj[[2]], c(NA)),listObj[[3]])



Is there any better way on how I can do that more directly?



Thanks and regards,


[[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.



__
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.


Re: [R] Question on list object

2011-04-27 Thread Henrique Dallazuanna
Try this:

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

On Wed, Apr 27, 2011 at 2:23 PM, Bogaso Christofer
bogaso.christo...@gmail.com wrote:
 Dear all, let say, I have following list object:



 listObj - vector(list, length = 3)

 listObj[[1]] - rnorm(3)

 listObj[[2]] - rnorm(4)

 listObj[[3]] - rnorm(5)



 Now I want to convert above list into a Matrix. Ofcourse I can do it using
 Reduce(rbind, listObj). However as you notice that as elements of that
 list are arbitrary length vectors, I cant use this trick. What I want is to
 have a matrix with 3x5 dimension, where the remaining element of each row
 with be filled with NA, i.e I want :



 rbind(c(listObj[[1]], c(NA, NA)), c(listObj[[2]], c(NA)),listObj[[3]])



 Is there any better way on how I can do that more directly?



 Thanks and regards,


        [[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.




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

__
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.