Hello,
I have for a long list of data.frames that I would like to get merged.
Each data.frame have two columns with the same names (Date and Value).
What I would like is for the objects in the list to be merged on Only
date with the column header being the name in the list.
For example
A <-
data.frame(Date=c("03/15/10","04/15/10","05/15/10"),
Value=c(1,2,3))
B <-
data.frame(Date=c("03/15/10","04/15/10","06/15/10"),
Value=c(5,5,5))
...
yoda <- list(A=A,B=B,...)
Result:
Date A B C
03/15/10 1 5 ...
04/15/10 2 5
05/15/10 3 NA
06/15/10 NA 5
Any ideas? I have been fiddling around with plyr and reshape without success
Thanks
Dan
--
**************************************************************
Daniel Brewer, Ph.D.
Institute of Cancer Research
Molecular Carcinogenesis
MUCRC
15 Cotswold Road
Sutton, Surrey SM2 5NG
United Kingdom
Tel: +44 (0) 20 8722 4109
**************************************************************
The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company
Limited by Guarantee, Registered in England under Company No. 534147 with its
Registered Office at 123 Old Brompton Road, London SW7 3RP.
This e-mail message is confidential and for use by the a...{{dropped:2}}
This will do it:> merge(A, B, by = 1, all = TRUE)Date Value.x Value.y 1 03/15/10 1 5 2 04/15/10 2 5 3 05/15/10 3 NA 4 06/15/10 NA 5 but you probably really want to be using time series for this so that you can easily perform other operations too. See ?merge.zoo and the three zoo vignettes for more.> library(zoo) > library(chron) > za <- zoo(A$Value, chron(as.character(A$Date))) > zb <- zoo(B$Value, chron(as.character(B$Date))) > merge(za, zb)za zb 03/15/10 1 5 04/15/10 2 5 05/15/10 3 NA 06/15/10 NA 5> zm <- merge(za, zb)> # convert to data frame > cbind(Date = time(zm), as.data.frame(zm))Date za zb 03/15/10 03/15/10 1 5 04/15/10 04/15/10 2 5 05/15/10 05/15/10 3 NA 06/15/10 06/15/10 NA 5> # handle more than two at a time > merge(za, za2 = za, zb)za za2 zb 03/15/10 1 1 5 04/15/10 2 2 5 05/15/10 3 3 NA 06/15/10 NA NA 5> # same but series are in a list > do.call(merge, list(za = za, za2 = za, zb = zb))za za2 zb 03/15/10 1 1 5 04/15/10 2 2 5 05/15/10 3 3 NA 06/15/10 NA NA 5 On Tue, Mar 9, 2010 at 11:21 AM, Daniel Brewer <daniel.brewer at icr.ac.uk> wrote:> Hello, > > I have for a long list of data.frames that I would like to get merged. > Each data.frame have two columns with the same names (Date and Value). > What I would like is for the objects in the list to be merged on Only > date with the column header being the name in the list. > > For example > > A <- data.frame(Date=c("03/15/10","04/15/10","05/15/10"), > Value=c(1,2,3)) > B <- data.frame(Date=c("03/15/10","04/15/10","06/15/10"), > Value=c(5,5,5)) > ... > > yoda <- list(A=A,B=B,...) > > Result: > > Date ? ? ? ? ? ?A ? ? ? B ? ? ? C > 03/15/10 ? ? ? ?1 ? ? ? 5 ? ? ? ... > 04/15/10 ? ? ? ?2 ? ? ? 5 > 05/15/10 ? ? ? ?3 ? ? ? NA > 06/15/10 ? ? ? ?NA ? ? ?5 > > Any ideas? I have been fiddling around with plyr and reshape without success > > Thanks > > Dan > > -- > ************************************************************** > > Daniel Brewer, Ph.D. > > Institute of Cancer Research > Molecular Carcinogenesis > MUCRC > 15 Cotswold Road > Sutton, Surrey SM2 5NG > United Kingdom > > Tel: +44 (0) 20 8722 4109 > > ************************************************************** > > The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. > > This e-mail message is confidential and for use by the a...{{dropped:2}} > > ______________________________________________ > 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. >
Daniel -
Reduce may be handy here:
ans = Reduce(function(a,b)merge(a,b,by=1),yoda)
names(ans)[2:ncol(ans)] = names(yoda)
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Tue, 9 Mar 2010, Daniel Brewer wrote:
> Hello,
>
> I have for a long list of data.frames that I would like to get merged.
> Each data.frame have two columns with the same names (Date and Value).
> What I would like is for the objects in the list to be merged on Only
> date with the column header being the name in the list.
>
> For example
>
> A <-
data.frame(Date=c("03/15/10","04/15/10","05/15/10"),
> Value=c(1,2,3))
> B <-
data.frame(Date=c("03/15/10","04/15/10","06/15/10"),
> Value=c(5,5,5))
> ...
>
> yoda <- list(A=A,B=B,...)
>
> Result:
>
> Date A B C
> 03/15/10 1 5 ...
> 04/15/10 2 5
> 05/15/10 3 NA
> 06/15/10 NA 5
>
> Any ideas? I have been fiddling around with plyr and reshape without
success
>
> Thanks
>
> Dan
>
> --
> **************************************************************
>
> Daniel Brewer, Ph.D.
>
> Institute of Cancer Research
> Molecular Carcinogenesis
> MUCRC
> 15 Cotswold Road
> Sutton, Surrey SM2 5NG
> United Kingdom
>
> Tel: +44 (0) 20 8722 4109
>
> **************************************************************
>
> The Institute of Cancer Research: Royal Cancer Hospital, a charitable
Company Limited by Guarantee, Registered in England under Company No. 534147
with its Registered Office at 123 Old Brompton Road, London SW7 3RP.
>
> This e-mail message is confidential and for use by the a...{{dropped:2}}
>
> ______________________________________________
> 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.
>