# [R] listing a sequence of vectors in a matrix

Gabor Grothendieck ggrothendieck at gmail.com
Tue Aug 22 16:00:52 CEST 2006

```Here is another variation using Robin's idea of t(sapply(...))

v <- c(3:5, 0)
t(sapply(lapply(v, function(n) seq(length = n)), "length<-", max(v) ))

# which can be shortened even further for the case where
# there are no zeros in v

v <- 3:5
t(sapply(lapply(v, seq), "length<-", max(v) ))

On 8/22/06, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:
> Gabor makes a good point about seq() vs a:b [a common gotcha
> for me].
>
>
> I'll revise my original function to:
>
>  > f <- function(a,n){(seq(length=a))[1:n]}
>  > t(sapply(c(2,3,4,4,4,5,6,0),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
> [8,]   NA   NA   NA   NA   NA
>  >
>
>
>
>
> rksh
>
> On 22 Aug 2006, at 13:55, Gabor Grothendieck wrote:
>
> > Here are two solutions.  seq(length = ...) instead of
> > just seq(...) is so that v can possibly contain zeros.
> >
> > # data
> > v <- 3:5
> >
> > # solution 1 - rbind/lapply
> > f <- function(n) {
> >       s = seq(length = n)
> >       replace(rep(NA, max(v)), s, s)
> > }
> > do.call(rbind, lapply(v, f))
> >
> > # solution 2 - loop
> > mat <- matrix(NA, length(v), max(v))
> > for(i in seq(v)) {
> >       s <- seq(length = v[i])
> >       mat[i, s] <- s
> > }
> >
> >
> > On 8/22/06, Sara-Jane Dunn <SND at bas.ac.uk> 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 only...
> >> {{dropped}}
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
>  tel  023-8059-7743
>
>

```