Re: [R] merging two data frame with colomns of different length
Try this: merge(transform(data1, .Id01 = ave(as.character(Id01), Id01, FUN=seq)), transform(data2, .Id02 = ave(as.character(Id02), Id02, FUN=seq)), by.x = c('Id01', '.Id01'), by.y = c('Id02', '.Id02'), all = TRUE) On Fri, Jul 31, 2009 at 3:05 PM, Julien Beguin wrote: > I forgot to mention that I tried with "merge" and "join" (package analogue) > without success... > > Julien > > Selon Julien Beguin , 31.07.2009: > > > Dear all, > > > > I am trying to merge two data frames based on a common column but for > this > > common column both data frame do not have the same length and associated > > information. I checked previous exemples in the list but was not able to > > apply > > them in my case... Is someone know how to do that? Below is my code with > the > > expected result: > > > > # data frame 1 > > Id1 <- c(1,1,1,2,2,2,3,3,3) > > Habit1 <- c('a','a','a','b','b','b','d','d','d') > > Id01 <- paste(Id1, Habit1,sep=".") > > data1 <- data.frame(Id01) > > > > # data frame 2 > > Id2 <- c(1,2,3,3) > > Habit2 <- c('a','b','d','d') > > Id02 <- paste(Id2, Habit2,sep=".") > > Area <- c(10,2,3,5) > > data2 <- data.frame(cbind(Id02, Area)) > > > > # result that I would like > > Area2 <- c(10, 0, 0, 2, 0, 0, 3, 5, 0) > > result <- data.frame(cbind(Id01, Area2)) > > > > Thank's > > > > -- > > > > Julien Beguin > > > > __ > > R-help@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. > > > > > -- > Julien Beguin > Etudiant au doctorat > Faculté de Foresterie et de Géomatique > Université Laval, local 2113 > 2405 rue de la Terrasse, G1V 0A6 Québec (Qc) > Tel: (418) 656-2131 poste 2620 > http://www.cen.ulaval.ca/anticosti/jbeguin.html > > __ > R-help@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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]] __ R-help@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.
Re: [R] merging two data frame with colomns of different length
Salut Julien, t1 <- table(data1$Id01) t2 <- table(data2$Id02) # compute difference between the two tables diff <- t1-t2 # create your data frame from data2 res <- data2 names(res)[1] <- "Id01" lsAdd <- rep(attributes(diff)$dimnames[[1]],diff) defVal <- NA rbind(res,data.frame(Id01 =lsAdd, Area=defVal)) Julien Beguin wrote: > > Dear all, > > I am trying to merge two data frames based on a common column but for this > common column both data frame do not have the same length and associated > information. I checked previous exemples in the list but was not able to > apply > them in my case... Is someone know how to do that? Below is my code with > the > expected result: > > # data frame 1 > Id1 <- c(1,1,1,2,2,2,3,3,3) > Habit1 <- c('a','a','a','b','b','b','d','d','d') > Id01 <- paste(Id1, Habit1,sep=".") > data1 <- data.frame(Id01) > > # data frame 2 > Id2 <- c(1,2,3,3) > Habit2 <- c('a','b','d','d') > Id02 <- paste(Id2, Habit2,sep=".") > Area <- c(10,2,3,5) > data2 <- data.frame(cbind(Id02, Area)) > > # result that I would like > Area2 <- c(10, 0, 0, 2, 0, 0, 3, 5, 0) > result <- data.frame(cbind(Id01, Area2)) > > Thank's > > -- > > Julien Beguin > > __ > R-help@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. > > -- View this message in context: http://www.nabble.com/merging-two-data-frame-with-colomns-of-different-length-tp24760153p24761975.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] merging two data frame with colomns of different length
I forgot to mention that I tried with "merge" and "join" (package analogue) without success... Julien Selon Julien Beguin , 31.07.2009: > Dear all, > > I am trying to merge two data frames based on a common column but for this > common column both data frame do not have the same length and associated > information. I checked previous exemples in the list but was not able to > apply > them in my case... Is someone know how to do that? Below is my code with the > expected result: > > # data frame 1 > Id1 <- c(1,1,1,2,2,2,3,3,3) > Habit1 <- c('a','a','a','b','b','b','d','d','d') > Id01 <- paste(Id1, Habit1,sep=".") > data1 <- data.frame(Id01) > > # data frame 2 > Id2 <- c(1,2,3,3) > Habit2 <- c('a','b','d','d') > Id02 <- paste(Id2, Habit2,sep=".") > Area <- c(10,2,3,5) > data2 <- data.frame(cbind(Id02, Area)) > > # result that I would like > Area2 <- c(10, 0, 0, 2, 0, 0, 3, 5, 0) > result <- data.frame(cbind(Id01, Area2)) > > Thank's > > -- > > Julien Beguin > > __ > R-help@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. > > -- Julien Beguin Etudiant au doctorat Faculté de Foresterie et de Géomatique Université Laval, local 2113 2405 rue de la Terrasse, G1V 0A6 Québec (Qc) Tel: (418) 656-2131 poste 2620 http://www.cen.ulaval.ca/anticosti/jbeguin.html __ R-help@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.