[R] Vector recycling and zoo

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 16 16:10:29 CEST 2010


When you combine zoo objects with arithmetic it merges them using all = FALSE:

> library(zoo)
> x <- data.frame(a=1:5*2, b=1:5*3)
> x <- zoo(x); x
   a  b
1  2  3
2  4  6
3  6  9
4  8 12
5 10 15
>
> # these two are the same
>
> x$a/x$a[1]
1
1
>
> m <- merge(x$a, x$a[1], all = FALSE)
> m
  x$a x$a[1]
1   2      2
> m[,1]/m[,2]
1
1





On Sun, May 16, 2010 at 3:00 AM, Sean Carmody <seancarmody at gmail.com> wrote:
> I am a bit confused about the different approaches taken to recycling in
> plain data frames and zoo objects. When carrying out simple arithmetic,
> dataframe seem to recycle single arguments, zoo objects do not. Here is an
> example
>
>> x <- data.frame(a=1:5*2, b=1:5*3)
>> x
>   a  b
> 1  2  3
> 2  4  6
> 3  6  9
> 4  8 12
> 5 10 15
>> x$a/x$a[1]
> [1] 1 2 3 4 5
>> x <- zoo(x)
>> x$a/x$a[1]
> 1
> 1
>>
>
> I feel understanding this difference would lead me to a greater
> understanding of the zoo module!
>
> Sean.
>
> --
> Sean Carmody
> Twitter: http://twitter.com/seancarmody
> Stable: http://mulestable.net/sean
>
> The Stubborn Mule
> Blog: http://www.stubbornmule.net
> Forum: http://mulestable.net/
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list