[R] calculations with vectors of unequal length

From: E Hofstadler <e.hofstadler_at_gmail.com>
Date: Wed, 27 Apr 2011 14:30:13 +0300

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?


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. Received on Wed 27 Apr 2011 - 11:38:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Apr 2011 - 13:00:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive