[R] joining "one-to-many"
eugen pircalabelu
eugen_pircalabelu at yahoo.com
Sun Feb 22 21:00:43 CET 2009
A not so elegant way of obtaining your result (compared to merge()) would be:
> t1$ val3<-rep(t2$val3, table(t1$loc))
> t1$ val4<-rep(t2$val4, table(t1$loc))
Eugen.
--- On Tue, 2/17/09, Monica Pisica <pisicandru at hotmail.com> wrote:
> From: Monica Pisica <pisicandru at hotmail.com>
> Subject: Re: [R] joining "one-to-many"
> To: ggrothendieck at gmail.com, marc_schwartz at comcast.net, h.wickham at gmail.com
> Cc: "R help project" <r-help at r-project.org>
> Date: Tuesday, February 17, 2009, 7:28 AM
> Ok,
>
> I feel properly ashamed. I suppose my "real" data
> is a little bit different than my toy data (although i
> don't know how) because i did try the merge function as
> simple as merge(t1, t2) and did not work. Maybe a reset of
> my session will solve my problems and more coffee my
> confusion.
>
> Again, thanks for your help,
>
> Monica
>
> ----------------------------------------
> > Date: Tue, 17 Feb 2009 10:09:17 -0500
> > Subject: Re: [R] joining "one-to-many"
> > From: ggrothendieck at gmail.com
> > To: pisicandru at hotmail.com
> > CC: r-help at r-project.org
> >
> > Try merge(t1, t2)
> >
> >
> > On Tue, Feb 17, 2009 at 9:33 AM, Monica Pisica wrote:
> >>
> >> Hello list,
> >>
> >> I am wondering if a joining
> "one-to-many" can be done a little bit easier. I
> tried merge function but I was not able to do it, so I end
> up using for and if.
> >>
> >> Suppose you have a table with locations, each
> location repeated several times, and some attributes at that
> location. The second table has the same locations, but only
> once with a different set of attributes. I would like to add
> the second set of attributes to the first table.
> >>
> >> Example:
> >>
> >> set.seed <- 123
> >> loc <- c(rep("L1", 3),
> rep("L2", 5), rep("L3", 2))
> >> val1 <- round(rnorm(10),2)
> >> val2 <- c("a", "b",
> "c", "a", "b", "d",
> "f", "e", "b", "e")
> >> t1 <- data.frame(loc, val1, val2)
> >> t2 <-
> data.frame(loc=c("L1","L2","L3"),
> val3 = c("m", "n", "p"), val4
> = c(25, 67, 48))
> >>
> >> # join one-to-many
> >>
> >> n <- nrow(t1)
> >> m <- nrow(t2)
> >> t1$val3 <- rep(1, n)
> >> t1$val4 <- rep(1, n)
> >>
> >> for (i in 1:n) {
> >> for (j in 1:m){
> >> if (t1$loc[i]==t2$loc[j]) {
> >> t1$val3[i] <- as.character(t2$val3[j])
> >> t1$val4[i] <- t2$val4[j]
> >> }
> >> }
> >> }
> >>
> >> Desired result:
> >>
> >> t1
> >> loc val1 val2 val3 val4
> >> 1 L1 -0.41 a m 25
> >> 2 L1 -0.69 b m 25
> >> 3 L1 0.36 c m 25
> >> 4 L2 1.11 a n 67
> >> 5 L2 0.15 b n 67
> >> 6 L2 -0.80 d n 67
> >> 7 L2 -0.08 f n 67
> >> 8 L2 -1.01 e n 67
> >> 9 L3 -1.01 b p 48
> >> 10 L3 -2.50 e p 48
> >>
> >>
> >> This code works OK but it is slow if the data
> frames are actually bigger than my little example. I hope
> somebody knows of a better way of doing these type of
> things.
> >>
> >> Thanks,
> >>
> >> Monica
> >>
> _________________________________________________________________
> >>
> >>
> >> 22009
> >> ______________________________________________
> >> 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.
> >>
>
> ______________________________________________
> 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