Re: [R] listing a sequence of vectors in a matrix

From: Robin Hankin <r.hankin_at_noc.soton.ac.uk>
Date: Tue 22 Aug 2006 - 22:58:47 EST

Hi

 > f <- function(a,n){(1:a)[1:n]}
 > t(sapply(c(2,3,4,4,4,5,6),f,n=5))

      [,1] [,2] [,3] [,4] [,5]

[1,]    1    2   NA   NA   NA
[2,]    1    2    3   NA   NA
[3,]    1    2    3    4   NA
[4,]    1    2    3    4   NA
[5,]    1    2    3    4   NA
[6,]    1    2    3    4    5
[7,]    1    2    3    4    5

 >
 >

HTH rksh

On 22 Aug 2006, at 12:29, Sara-Jane Dunn wrote:

> Hi,
>
> I'm having trouble applying the matrix function. I'd like to be
> able to
> create a matrix of vectors filled in by rows, which are not all the
> same
> length, and so I need it to fill in NAs where applicable.
>
> It's easiest to explain with a simple example:
>
> Suppose vec = c(3,4,5). How can I form a matrix of the vectors 1:vec
> [j]
> for j=1:3?
> i.e. 1 2 3 NA NA
> 1 2 3 4 NA
> 1 2 3 4 5
> I've tried matrix(c(1:vec[j]),nrow=max(j),ncol=max(vec)) but it will
> only give me a matrix with repeated values for j=1, like 1 2 3 1
> 2
> 3 1 2 3 1
> 2 3 1 2 3
>
> Also using the list function hasn't got me anywhere either..
>
> Any help/ideas would be greatly appreciated!
>
> Many thanks,
> Sara-Jane Dunn
>
> --
> This message (and any attachments) is for the recipient on...{{dropped}}



R-help@stat.math.ethz.ch 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. Received on Wed Aug 23 00:25:19 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 23 Aug 2006 - 02:22:03 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.