[R] Is there anywhere recycle()?

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jul 23 18:27:16 CEST 2006


Here is another possibility:

rep(a, length = length(x))

On 7/23/06, Gregor Gorjanc <gregor.gorjanc at gmail.com> wrote:
> Hi,
>
> Gabor Grothendieck wrote:
> > Try:
> >
> > foo2 <- function(x, a) cbind(x,a)[,2]
> >
>
> thank you for this. It does work to some extent, but not much better
> than mine foo.
>
> foo2(c(1, 2, 3), a=1)
> [1] 1 1 1
>
> 18:14:08
> R> foo2(c(1, 2, 3), a=c(1,2,3,4))
> [1] 1 2 3 4
> Warning message:
> number of rows of result
>        is not a multiple of vector length (arg 1) in: cbind(1, x, a)
>
> 18:14:13
> R> foo2(c(1, 2, 3), a=c(1,2,3))
> [1] 1 2 3
>
> 18:14:18
> R> foo2(c(1, 2, 3), a=c(1,2))
> [1] 1 2 1
> Warning message:
> number of rows of result
>        is not a multiple of vector length (arg 2) in: cbind(1, x, a)
>
>
>
> > On 7/23/06, Gregor Gorjanc <gregor.gorjanc at gmail.com> wrote:
> >> Hello!
> >>
> >> I am writting a function, which should recycle one of its arguments if
> >> length of the argument is approprate i.e. something like
> >>
> >> foo <- function(x, a)
> >> {
> >>  n <- length(x)
> >>  if(length(a) < n) { # recycle a
> >>    oldA <- a
> >>    a <- vector(length=n)
> >>    a[1:n] <- oldA
> >>  }
> >>  ## ...
> >>  return(a)
> >> }
> >>
> >> foo(c(1, 2), a=c(1, 2))
> >> foo(c(1, 2), a=c(1))
> >>
> >> I am now wondering if there is any general/generic functions for such
> >> task.
> >>
> >> Thanks!
> >>
> >> --
> >> Lep pozdrav / With regards,
> >>    Gregor Gorjanc
> >>
> >> ----------------------------------------------------------------------
> >> University of Ljubljana     PhD student
> >> Biotechnical Faculty
> >> Zootechnical Department     URI: http://www.bfro.uni-lj.si/MR/ggorjan
> >> Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si
> >>
> >> SI-1230 Domzale             tel: +386 (0)1 72 17 861
> >> Slovenia, Europe            fax: +386 (0)1 72 17 888
> >>
> >> ----------------------------------------------------------------------
> >> "One must learn by doing the thing; for though you think you know it,
> >>  you have no certainty until you try." Sophocles ~ 450 B.C.
> >>
> >> ______________________________________________
> >> R-help at 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.
> >>
>
>
> --
> Lep pozdrav / With regards,
>    Gregor Gorjanc
>
> ----------------------------------------------------------------------
> University of Ljubljana     PhD student
> Biotechnical Faculty
> Zootechnical Department     URI: http://www.bfro.uni-lj.si/MR/ggorjan
> Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si
>
> SI-1230 Domzale             tel: +386 (0)1 72 17 861
> Slovenia, Europe            fax: +386 (0)1 72 17 888
>
> ----------------------------------------------------------------------
> "One must learn by doing the thing; for though you think you know it,
>  you have no certainty until you try." Sophocles ~ 450 B.C.
> ----------------------------------------------------------------------
>



More information about the R-help mailing list