Thank you very much for the reply. The thing I want to achieve in the end is to use dataframe1 as a master dataframe, and get values from multiple dataframes to dataframe1, so that I can analyze the data altogether, like this:
ID value1 value2 value3 a 100 123 456 b 321 654 c 200 564 I first created an empty column in dataframe1, and then I tried to record the values into this column at the corresponding rows, but I had a problem. I was not able to tell R "please do nothing when there's not a match". DF1<-transform(DF1, value1=rep(NA, nrow(DF1))) for (i in seq(1:nrow(DF1))) { if (is.numeric(DF2[grep(DF1[i, "ID"], DF2$ID), "value"])==T) {DF1<-transform(DF1, value1[i]<-DF2[grep(DF1[i, "ID"], DF2$ID), "value"])} else {print("bingo!")} } but the is.numeric() test was TRUE even when there was not a match, and it resulted in an error. If I do it reversely, I'll get all "bingo!" for (i in seq(1:nrow(DF1))) { if (is.numeric(DF2[grep(DF1[i, "ID"], DF2$ID), "value"])==T) {print("bingo!")} else {DF1<-transform(DF1, value1[i]<-DF2[grep(DF1[i, "ID"], DF2$ID), "value"])} } Any ideas how I can get what I wanted? Henrique Dallazuanna wrote: > > Try this: > g <- sapply(DF1$ID, grep, x = DF2$ID) > transform(DF2[unlist(g),],ID = DF1$ID[which(g > 0)]) > > On Thu, Aug 6, 2009 at 1:41 PM, Rnewbie <xua...@yahoo.com> wrote: > >> >> dear all, >> >> I have two dataframes >> >> dataframe1 >> ID >> a >> b >> c >> >> dataframe2 >> ID value >> a;W 100 >> X;c 200 >> Y;Z 300 >> >> I wanted to match the IDs from the two dataframes and record the values >> into >> a new column of dataframe1 at the corresponding rows. This is what I >> expect: >> >> dataframe1 >> ID value >> a 100 >> b >> c 200 >> >> I tried doing it like this: >> >> for (i in seq(1:nrow(dataframe1))) { >> dataframe1[i,"value"]<-dataframe2[grep(dataframe1[i,"ID"], >> dataframe2$ID),"value"] >> } >> >> but I failed. I was able to extracted the values from dataframe2 but not >> able to record the values in the corresponding rows of dataframe1. >> >> I would appreciate any suggestions. Thanks in advance. >> >> Jim >> -- >> View this message in context: >> http://www.nabble.com/problem-with-recording-numeric-output-into-another-dataframe-tp24850155p24850155.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. >> > > > > -- > 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. > > -- View this message in context: http://www.nabble.com/problem-with-recording-numeric-output-into-another-dataframe-tp24850155p24865628.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.