Dear r-users, Suppose I have three datasets: Dataset-1: Date x y Jan-1,2005 120 230 Jan-2,2005 123 -125 Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-7,2005 11 299 Mar-5,2005 200 311 Dataset-2: Date x y Jan-2,2005 123 -125 Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-5,2005 11 299 Jan-6,2005 -23 12 Mar-5,2005 200 311 Dataset-3: Date x y Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-5,2005 11 299 Mar-5,2005 200 311 Apl-23,2005 123 200 Now I want to get the common dates along with x and y from this above three datasets keeping the same order in date-variable as it is. For ex. I want to get: Date x y x y x y (from dataset-1) (from dataset-2) (from dataset-3) ------------------------------------------------------------------------------------------------ Jan-3,2005 -110 300 -110 300 -110 300 Jan-4,2005 114 -21 114 -21 114 -21 Mar-5,2005 200 311 200 311 200 311 Can anyone give me any R code to implement this for any number of datasets ? Thanks and regards thanks in advance --------------------------------- [[alternative HTML version deleted]]
Read them in as zoo objects (you can replace textConnection(Lines1) with the filename) and then merge them using all = FALSE to retain only common time points. Note that in my English locale I had to modify your Apl to Apr. Lines1 <- "Date x y Jan-1,2005 120 230 Jan-2,2005 123 -125 Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-7,2005 11 299 Mar-5,2005 200 311" Lines2 <- "Date x y Jan-2,2005 123 -125 Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-5,2005 11 299 Jan-6,2005 -23 12 Mar-5,2005 200 311" Lines3 <- "Date x y Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-5,2005 11 299 Mar-5,2005 200 311 Apr-23,2005 123 200" library(zoo) DF1 <- read.zoo(textConnection(Lines1), header = TRUE, format = "%b-%d,%Y") DF2 <- read.zoo(textConnection(Lines2), header = TRUE, format = "%b-%d,%Y") DF3 <- read.zoo(textConnection(Lines3), header = TRUE, format = "%b-%d,%Y") merge(DF1, DF2, DF3, all = FALSE) On 4/20/06, stat stat <stat700004 at yahoo.co.in> wrote:> Dear r-users, > > Suppose I have three datasets: > Dataset-1: > Date x y > Jan-1,2005 120 230 > Jan-2,2005 123 -125 > Jan-3,2005 -110 300 > Jan-4,2005 114 -21 > Jan-7,2005 11 299 > Mar-5,2005 200 311 > > Dataset-2: > Date x y > Jan-2,2005 123 -125 > Jan-3,2005 -110 300 > Jan-4,2005 114 -21 > Jan-5,2005 11 299 > Jan-6,2005 -23 12 > Mar-5,2005 200 311 > > Dataset-3: > Date x y > Jan-3,2005 -110 300 > Jan-4,2005 114 -21 > Jan-5,2005 11 299 > Mar-5,2005 200 311 > Apl-23,2005 123 200 > Now I want to get the common dates along with x and y from this above three datasets keeping the same order > in date-variable as it is. > For ex. I want to get: > Date x y x y x y > (from dataset-1) (from dataset-2) (from dataset-3) > ------------------------------------------------------------------------------------------------ > Jan-3,2005 -110 300 -110 300 -110 300 > Jan-4,2005 114 -21 114 -21 114 -21 > Mar-5,2005 200 311 200 311 200 311 > Can anyone give me any R code to implement this for any number of datasets ? > Thanks and regards > > > thanks in advance > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >
I think this does what you require. #Read your data in whatever way you wish: d1<-data.frame(Date=c("2005/1/1","2005/2/1","2005/1/3","2005/1/4","2005/ 1/7","2005/3/5"), x=c(119,123,-110,114,11,200), y=c(230,-125,300,-21,299,311)) d2<-data.frame(Date=c("2005/1/3","2005/1/4","2005/1/5","2005/1/6","2005/ 3/5"), x=c(-220,116,888,-239,201), y=c(301,-23,3000,122,312)) d3<-data.frame(Date=c("2005/1/4","2005/1/5","2005/3/5","2005/4/23"), x=c(392,511,600,723), y=c(-81,6699,9311,1200)) #Make a list listof<-list(d1,d2,d3) #loop over any number of datasets merging as you go for ( dataset in 1:length(listof)-1) { if (dataset == 1) { res<-merge(listof[dataset],listof[dataset+1],all=T,by="Date") } else { res<-merge(res,listof[dataset+1],all=T,by="Date") } } # Hope that helps JS --- John Seers Institute of Food Research Norwich Research Park Colney Norwich NR4 7UA tel +44 (0)1603 251490 fax +44 (0)1603 255167 e-mail john.seers at bbsrc.ac.uk e-disclaimer at http://www.ifr.ac.uk/edisclaimer/ Web sites: www.ifr.ac.uk www.foodandhealthnetwork.com -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of stat stat Sent: 20 April 2006 09:17 To: r-help at stat.math.ethz.ch Subject: [R] R-Help Dear r-users, Suppose I have three datasets: Dataset-1: Date x y Jan-1,2005 120 230 Jan-2,2005 123 -125 Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-7,2005 11 299 Mar-5,2005 200 311 Dataset-2: Date x y Jan-2,2005 123 -125 Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-5,2005 11 299 Jan-6,2005 -23 12 Mar-5,2005 200 311 Dataset-3: Date x y Jan-3,2005 -110 300 Jan-4,2005 114 -21 Jan-5,2005 11 299 Mar-5,2005 200 311 Apl-23,2005 123 200 Now I want to get the common dates along with x and y from this above three datasets keeping the same order in date-variable as it is. For ex. I want to get: Date x y x y x y (from dataset-1) (from dataset-2) (from dataset-3) ------------------------------------------------------------------------ ------------------------ Jan-3,2005 -110 300 -110 300 -110 300 Jan-4,2005 114 -21 114 -21 114 -21 Mar-5,2005 200 311 200 311 200 311 Can anyone give me any R code to implement this for any number of datasets ? Thanks and regards thanks in advance --------------------------------- [[alternative HTML version deleted]] ______________________________________________ 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