[R] Merging "list" object

Gabor Grothendieck ggrothendieck at gmail.com
Tue Apr 13 13:16:27 CEST 2010


There is no facility to pass additional arguments to your function in
Reduce.  See ?Reduce

zoo's merge function can merge multiple series.  You don't need Reduce.

Any of these three should work:

library(zoo)
dat <- vector("list")
for (i in 1:4) dat[[i]] <- zooreg(rnorm(i*10),
	start=as.Date("2000-01-01"), frequency=1)
names(dat) <- letters[1:4]

# 1
z1 <- do.call(merge, c(dat, all = FALSE))

# 2
z2 <- merge(a = dat[[1]], b = dat[[2]], c = dat[[3]], d = dat[[4]], all = FALSE)

# 3
z3 <- Reduce(function(x, y) merge(x, y, all = FALSE), dat)
colnames(z3) <- names(dat)

identical(z1, z2) # TRUE

time(z1) <- unname(time(z1))
identical(z1, z3) # TRUE



On Tue, Apr 13, 2010 at 4:19 AM, Ron_M <ron_michael70 at yahoo.com> wrote:
>
> Hi, I have created following "list" object :
>
>> library(zoo)
>> dat <- vector("list")
>> for (i in 1:4) dat[[i]] <- zooreg(rnorm(i*10),
>> start=as.Date("2000-01-01"), frequency=1)
>
>> dat[[1]]
> 2000-01-01 2000-01-02 2000-01-03 2000-01-04 2000-01-05 2000-01-06 2000-01-07
> 2000-01-08 2000-01-09 2000-01-10
> -0.7023352 -0.2063284 -1.0684688 -0.5345360  1.0045435 -0.6528107  1.0922850
> 0.6592155  0.1889817 -0.5154872
>> dat[[2]]
>  2000-01-01  2000-01-02  2000-01-03  2000-01-04  2000-01-05  2000-01-06
> 2000-01-07  2000-01-08  2000-01-09  2000-01-10
> -1.33096001 -0.41484364  0.03850233 -0.78752649  1.50566685 -1.05094523
> 0.58845956  0.28183515  0.90528437 -0.61535069
>  2000-01-11  2000-01-12  2000-01-13  2000-01-14  2000-01-15  2000-01-16
> 2000-01-17  2000-01-18  2000-01-19  2000-01-20
> -0.76326498 -0.38750503  0.59519399 -0.39464736  0.80053668  0.56286659
> 0.03991622  1.20224474 -0.11436020  0.79030262
>> dat[[3]]
>  2000-01-01   2000-01-02   2000-01-03   2000-01-04   2000-01-05
> 2000-01-06   2000-01-07   2000-01-08   2000-01-09
> -0.939523920  0.679475552 -0.248264399  0.658726986 -0.154457503
> -0.327342956  1.358551653 -0.051436045  0.455469878
>  2000-01-10   2000-01-11   2000-01-12   2000-01-13   2000-01-14
> 2000-01-15   2000-01-16   2000-01-17   2000-01-18
>  2.982439031  0.499840045  0.009528193  0.372458627 -0.762186202
> -0.988996299 -0.673831659 -0.443536816  0.413445687
>  2000-01-19   2000-01-20   2000-01-21   2000-01-22   2000-01-23
> 2000-01-24   2000-01-25   2000-01-26   2000-01-27
> -0.069849302  0.520691745 -0.296890679  2.093093754  0.696635791
> -0.286680977 -0.354431757 -0.157794595 -2.247776154
>  2000-01-28   2000-01-29   2000-01-30
> -1.429291735 -0.302251311  0.071380311
>> dat[[4]]
>  2000-01-01   2000-01-02   2000-01-03   2000-01-04   2000-01-05
> 2000-01-06   2000-01-07   2000-01-08   2000-01-09
>  1.229357467 -1.231028063 -0.384540478  1.058490237 -0.275243948
> 0.097183122 -1.768885144 -0.611407839 -3.441311777
>  2000-01-10   2000-01-11   2000-01-12   2000-01-13   2000-01-14
> 2000-01-15   2000-01-16   2000-01-17   2000-01-18
> -0.312644935  0.380036670  0.025232772 -0.090080369 -1.157750720
> 0.859936199  0.590191927 -0.824260485 -1.570709392
>  2000-01-19   2000-01-20   2000-01-21   2000-01-22   2000-01-23
> 2000-01-24   2000-01-25   2000-01-26   2000-01-27
> -1.356200595 -0.163619627  1.680948233  2.045026560  1.756296234
> -1.422084889 -0.075294543  1.986966298  0.171919466
>  2000-01-28   2000-01-29   2000-01-30   2000-01-31   2000-02-01
> 2000-02-02   2000-02-03   2000-02-04   2000-02-05
>  0.474975000 -1.226787467  0.508195208 -1.237140935  0.361799136
> 0.497959878 -0.857453525  2.833531013  0.396793937
>  2000-02-06   2000-02-07   2000-02-08   2000-02-09
> -0.966378375  0.556724596  0.170147308 -0.005016784
>
>
> Now I want to merge all these 4 time series with "all=FALSE". I have tried
> with Reduce() function, However could not get desired result :
>
>> tail(Reduce("merge", dat))
>           init x[[i]] x[[i]].1     x[[i]].2
> 2000-02-04   NA     NA       NA  2.833531013
> 2000-02-05   NA     NA       NA  0.396793937
> 2000-02-06   NA     NA       NA -0.966378375
> 2000-02-07   NA     NA       NA  0.556724596
> 2000-02-08   NA     NA       NA  0.170147308
> 2000-02-09   NA     NA       NA -0.005016784
>
>
> If I all additinal argument for merge() function I get error :
>
>> tail(Reduce("merge", all=F, dat))
> Error in Reduce("merge", all = F, dat) : unused argument(s) (all = F)
>
> How to force R to report the data available only for all days?
>
> Thanks,
> --
> View this message in context: http://n4.nabble.com/Merging-list-object-tp1838111p1838111.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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