I am positive this problem has a very simple solution, but I have been unable to find it, so I am asking for your help. I need to know how to look something up in one data frame and add it as a column in another. If I have a data frame that looks like this:> frame1ID score test age 1 Guy1 10 1 20 2 Guy1 13 2 20 3 Guy2 9 1 33 4 Guy2 11 2 33 and another frame that looks like this:> frame2ID 1 Guy1 2 Guy2 How do I add a column to frame2 so it looks like this: ID age 1 Guy1 20 2 Guy2 33 I know this must be simple, but I couldn't find the solution by searching. thanks so much Jeremy -- View this message in context: http://r.789695.n4.nabble.com/add-column-with-values-found-in-another-data-frame-tp4295626p4295626.html Sent from the R help mailing list archive at Nabble.com.
Jorge I Velez
2012-Jan-15 04:32 UTC
[R] add column with values found in another data frame
Hi Jeremy, Try> frame1 <- structure(list(ID = structure(c(1L, 1L, 2L, 2L), .Label c("Guy1","Guy2"), class = "factor"), score = c(10L, 13L, 9L, 11L), test = c(1L, 2L, 1L, 2L), age = c(20L, 20L, 33L, 33L)), .Names = c("ID", "score", "test", "age"), class = "data.frame", row.names = c("1", "2", "3", "4"))> frame2 <- structure(list(ID = structure(1:2, .Label = c("Guy1", "Guy2"),class = "factor")), .Names = "ID", class = "data.frame", row.names = c("1", "2"))> merge(frame1, frame2, by = "ID"))ID score test age 1 Guy1 10 1 20 2 Guy1 13 2 20 3 Guy2 9 1 33 4 Guy2 11 2 33> subset(frame1, ID %in% frame2$ID, select = c(ID, age))ID age 1 Guy1 20 2 Guy1 20 3 Guy2 33 4 Guy2 33 See ?subset and ?merge for more information. HTH, Jorge.- On Sat, Jan 14, 2012 at 3:51 PM, jdog76 <> wrote:> I am positive this problem has a very simple solution, but I have been > unable to find it, so I am asking for your help. I need to know how to look > something up in one data frame and add it as a column in another. If I > have > a data frame that looks like this: > > > frame1 > ID score test age > 1 Guy1 10 1 20 > 2 Guy1 13 2 20 > 3 Guy2 9 1 33 > 4 Guy2 11 2 33 > > and another frame that looks like this: > > > frame2 > ID > 1 Guy1 > 2 Guy2 > > How do I add a column to frame2 so it looks like this: > > ID age > 1 Guy1 20 > 2 Guy2 33 > > I know this must be simple, but I couldn't find the solution by searching. > > thanks so much > Jeremy > > > -- > View this message in context: > http://r.789695.n4.nabble.com/add-column-with-values-found-in-another-data-frame-tp4295626p4295626.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. >[[alternative HTML version deleted]]
Pete Brecknock
2012-Jan-15 04:58 UTC
[R] add column with values found in another data frame
jdog76 wrote> > I am positive this problem has a very simple solution, but I have been > unable to find it, so I am asking for your help. I need to know how to > look something up in one data frame and add it as a column in another. If > I have a data frame that looks like this: > >> frame1 > ID score test age > 1 Guy1 10 1 20 > 2 Guy1 13 2 20 > 3 Guy2 9 1 33 > 4 Guy2 11 2 33 > > and another frame that looks like this: > >> frame2 > ID > 1 Guy1 > 2 Guy2 > > How do I add a column to frame2 so it looks like this: > > ID age > 1 Guy1 20 > 2 Guy2 33 > > I know this must be simple, but I couldn't find the solution by searching. > > thanks so much > Jeremy >How about .... frame2$age = frame1[match(frame2$ID, frame1$ID),"age"] print(frame2) ID age 1 Guy1 20 2 Guy2 33 HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/add-column-with-values-found-in-another-data-frame-tp4295626p4296307.html Sent from the R help mailing list archive at Nabble.com.
Thanks so much Jorge and Pete ! Pete Brecknock wrote> > > frame2$age = frame1[match(frame2$ID, frame1$ID),"age"] > >This match function was exactly what I needed to know! -- View this message in context: http://r.789695.n4.nabble.com/add-column-with-values-found-in-another-data-frame-tp4295626p4297417.html Sent from the R help mailing list archive at Nabble.com.