I am trying to create a new variable using ifelse by combining data from two data.frames (similar to this question but without factors) My problem is that df1 features yearly data, whereas vars in df2 are temporally aggregated: e.g. df1 has multiple obs (1997,1998,...,2005) and df2 only has a range (1900-2001). For illustration, a 2x2 example would look like
df1$id <- c("2","20")
df1$year <- c("1960","1870")
df2$id <- df1$id
df2$styear <- c("1800","1900")
df2$endyear <- c("2001","1950")
I want to combine both in such a way that the id (same variable exists in both) is matched, and further, the year in df1 is within the range of df2. I tried the following
df1$new.var <- ifelse(df1$id==df2$id & df1$year>=df2$styear &
df1$year<df2$endyear,1,0)
which ideally should return 1 and 0, respectively.But instead I get
Warning messages:
1: In df1$id == df2$id : longer object length is not a multiple of shorter object length
2: In df1$year >= df2$styear : longer object length is not a multiple of shorter object length
3: In df1$year < df2$endyear : longer object length is not a multiple of shorter object length
For the record, the 'real' df1 has 500 obs and df2 has 14. How can I make this work?
Aucun commentaire:
Enregistrer un commentaire