Hi there, this is probably simple but I can't seem to figure it out by myself... I have two dataframes (df.1 and df.2): df.1 <- data.frame(year=factor(rep(1:3,3)), level=rep(letters[1:3],3), number=c(11:19)) df.2 <- data.frame(year=factor(c(1:5)), number=c(21:25)) I would like to create a new variable df.1$new, which is supposed to be the sum of each element of df.1$number and those elements of df.2$number, where df.2$year equals df.1$year. What would be the most efficient way of doing this? Regards, EH
See ?merge. df.1 <- data.frame(year=factor(rep(1:3,3)), level=rep(letters[1:3],3), number=c(11:19)) df.2 <- data.frame(year=factor(c(1:5)), number=c(21:25)) df.3 <- merge(df.1, df.2, by = "year") df.3$new <- with(df.3, number.x + number.y) Jeremy On Wednesday, April 27, 2011 7:30:13 AM UTC-4, E Hofstadler wrote:> > Hi there, > > this is probably simple but I can't seem to figure it out by myself... > > I have two dataframes (df.1 and df.2): > > df.1 <- data.frame(year=factor(rep(1:3,3)), level=rep(letters[1:3],3), > number=c(11:19)) > df.2 <- data.frame(year=factor(c(1:5)), number=c(21:25)) > > I would like to create a new variable df.1$new, which is supposed to > be the sum of each element of df.1$number and those elements of > df.2$number, where df.2$year equals df.1$year. > > What would be the most efficient way of doing this? > > Regards, > EH > > ______________________________________________ > R-h... 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. > >
> df.1 <- merge(df.1, df.2, by = "year") > df.1$sum <- df.1$number.x + df.1$number.y > df.1 year level number.x number.y sum 1 1 a 11 21 32 2 1 a 17 21 38 3 1 a 14 21 35 4 2 b 12 22 34 5 2 b 15 22 37 6 2 b 18 22 40 7 3 c 16 23 39 8 3 c 13 23 36 9 3 c 19 23 42Scott On Wednesday, April 27, 2011 at 6:30 AM, E Hofstadler wrote:> Hi there, > > this is probably simple but I can't seem to figure it out by myself... > > I have two dataframes (df.1 and df.2): > > df.1 <- data.frame(year=factor(rep(1:3,3)), level=rep(letters[1:3],3), > number=c(11:19)) > df.2 <- data.frame(year=factor(c(1:5)), number=c(21:25)) > > I would like to create a new variable df.1$new, which is supposed to > be the sum of each element of df.1$number and those elements of > df.2$number, where df.2$year equals df.1$year. > > What would be the most efficient way of doing this? > > Regards, > EH > > ______________________________________________ > 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]]
Hi r-help-bounces at r-project.org napsal dne 27.04.2011 13:30:13:> Hi there, > > this is probably simple but I can't seem to figure it out by myself... > > I have two dataframes (df.1 and df.2): > > df.1 <- data.frame(year=factor(rep(1:3,3)), level=rep(letters[1:3],3), > number=c(11:19)) > df.2 <- data.frame(year=factor(c(1:5)), number=c(21:25)) > > I would like to create a new variable df.1$new, which is supposed to > be the sum of each element of df.1$number and those elements of > df.2$number, where df.2$year equals df.1$year.merge(df.1,df.2, by.x="year", by.y="year", all.x=T) year level number.x number.y 1 1 a 11 21 2 1 a 17 21 3 1 a 14 21 than you can sum last 2 columns. I believe that sqldf package can do what you want more efficiently but I do not use it so I do not know exact syntax. Regards Petr> > What would be the most efficient way of doing this? > > Regards, > EH > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.