Reputation: 185
I am attempting to join two data frames by a variable that has a different name and by a single conditional criterion (i.e. the year) in the source.
Investigating various solutions has not provided me with an answer.
Data:
x_6.0_2011 <- data.frame(t = c("1", "2", "3", "7"), value_1 = c("0.9", "0.6", "0.1", "0.8"))
x <- data.frame(year = c("2010", "2011", "2012", "2013"), t.nr = c("1", "1", "2", "7"), value = c("0.2", "0.5", "0.7", "0.3"))
What I tried:
x2011 <- ifelse(x$year == '2011',
left_join(x, x_6.0_2011, by = c('t.nr' = 't')), 0)
--> producing a list - so wrong solution path
x20111 <- left_join(x, x_6.0_2011, by = c('t.nr' = 't'), ,year== 2011 )
--> producing a df but only replacing the correct value and coyping in the remainders from the x_6.0_2011 - so wrong solution path
xx_6.02011 <- left_join(filter(x, year == '2011'), x_6.0_2011, by = c('t.nr' = 't'))
--> producing df with one line but I would like to have kept the other entries not containing any value
Result Expected:
Yr t.nr value value_1
2010 1 0.2 0
2011 1 0.3 0.9
2012 2 0.7 0
2013 7 0.3 0
... or similar at least in the last column not a value I cannot deduces.
The minimal example works however my working project (217 variable) still produces an error:
I do consistently get: Error: data_frames can only contain 1d atomic vectors and lists
Any advice is more than appreciated. Or are my attempts to simplistic?
Upvotes: 2
Views: 3245
Reputation: 887731
You could use the devel
version of data.table
library(data.table)#v1.9.5+
setDT(df1)[df2, on=c('t'='t.nr')][year!=2011, value_1:='0'][]
# t value_1 year value
#1: 1 0 2010 0.2
#2: 1 0.9 2011 0.5
#3: 2 0 2012 0.7
#4: 7 0 2013 0.3
Upvotes: 4