Philipp Chapkovski
2011-May-29 20:51 UTC
[R] how to combine two data frames via factors (or somehow else)
Hello! The question should be very naive, but I am a beginner and stuck with this unfortunately. There is a dataset of people's affiliation to companies that looks like that (a data frame actually) x y 1 a X 2 b X 3 c X 4 f Z 5 e Z 6 g Z where x is a name of a person and y is a company name. That is, a,b,c work in a company X. f,e,g work in a company Z. and there is another dataset with the affiliation of companies. Like that (much much longer in reality): y1 y2 1 X W 2 Z W that means that both X and Z belongs to the same company W. What is the most compact way (without loops etc) to 'attach' the 'head company' from the second dataset to the first dataset so I can have something like: x y owner 1 a X W ... 4 f Z W I know it should be done somehow with factors, but I don't know how. Thank you in advance!
Sarah Goslee
2011-May-29 21:11 UTC
[R] how to combine two data frames via factors (or somehow else)
merge() should do the trick. Using strings instead of factors works better, though. Sarah On Sun, May 29, 2011 at 4:51 PM, Philipp Chapkovski <chapkovski at gmail.com> wrote:> Hello! > The question should be very naive, but I am a beginner and stuck with > this unfortunately. > There is a dataset of people's affiliation to companies that looks > like that (a data frame actually) > ?x y > 1 a X > 2 b X > 3 c X > 4 f Z > 5 e Z > 6 g Z > where x is a name of a person and y is a company name. That is, a,b,c > work in a company X. f,e,g work in a company Z. > > and there is another dataset with the affiliation of companies. Like > that (much much longer in reality): > ?y1 y2 > 1 ?X W > 2 ?Z W > that means that both X and Z belongs to the same company W. > What is the most compact way (without loops etc) to 'attach' the 'head > company' from the second dataset to the first dataset so I can have > something like: > > ?x ?y owner > 1 a X W > ... > 4 f ?Z W > > I know it should be done somehow with factors, but I don't know how. > Thank you in advance! >-- Sarah Goslee http://www.functionaldiversity.org
Phil Spector
2011-May-29 21:12 UTC
[R] how to combine two data frames via factors (or somehow else)
Philipp - I believe you're looking for the merge function. If you need more guidance, please provide a meaningful reproducible example. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Mon, 30 May 2011, Philipp Chapkovski wrote:> Hello! > The question should be very naive, but I am a beginner and stuck with > this unfortunately. > There is a dataset of people's affiliation to companies that looks > like that (a data frame actually) > x y > 1 a X > 2 b X > 3 c X > 4 f Z > 5 e Z > 6 g Z > where x is a name of a person and y is a company name. That is, a,b,c > work in a company X. f,e,g work in a company Z. > > and there is another dataset with the affiliation of companies. Like > that (much much longer in reality): > y1 y2 > 1 X W > 2 Z W > that means that both X and Z belongs to the same company W. > What is the most compact way (without loops etc) to 'attach' the 'head > company' from the second dataset to the first dataset so I can have > something like: > > x y owner > 1 a X W > ... > 4 f Z W > > I know it should be done somehow with factors, but I don't know how. > Thank you in advance! > > ______________________________________________ > 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. >
Joshua Wiley
2011-May-29 21:21 UTC
[R] how to combine two data frames via factors (or somehow else)
Hi Philipp, ## Read in your data ## posting the output of dput() would have made our lives easier d1 <- read.table(textConnection("x y 1 a X 2 b X 3 c X 4 f Z 5 e Z 6 g Z"), header = TRUE, row.names = 1) d2 <- read.table(textConnection(" y1 y2 1 X W 2 Z W"), header = TRUE, row.names = 1) closeAllConnections() ## Solution using merge() dboth <- merge(d1, d2, by.x = "y", by.y = "y1") ## an alternate solution in this case d1.alt <- d1 d1.alt$owner <- d2[d2$y1[d1$y], "y2"] ## Compare them dboth d1.alt Hope this helps, Josh On Sun, May 29, 2011 at 1:51 PM, Philipp Chapkovski <chapkovski at gmail.com> wrote:> Hello! > The question should be very naive, but I am a beginner and stuck with > this unfortunately. > There is a dataset of people's affiliation to companies that looks > like that (a data frame actually) > ?x y > 1 a X > 2 b X > 3 c X > 4 f Z > 5 e Z > 6 g Z > where x is a name of a person and y is a company name. That is, a,b,c > work in a company X. f,e,g work in a company Z. > > and there is another dataset with the affiliation of companies. Like > that (much much longer in reality): > ?y1 y2 > 1 ?X W > 2 ?Z W > that means that both X and Z belongs to the same company W. > What is the most compact way (without loops etc) to 'attach' the 'head > company' from the second dataset to the first dataset so I can have > something like: > > ?x ?y owner > 1 a X W > ... > 4 f ?Z W > > I know it should be done somehow with factors, but I don't know how. > Thank you in advance! > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/