I have a dataframe like so:
rel <- c(2, 5, NA, 3, 6)
year.in <- c(4, NA, 2, 3, 2)
year.out <- c(6, 7, NA, 5, 4)
year.1 <- c(NA, NA, NA, NA, NA)
year.2 <- c(NA, NA, NA, NA, NA)
year.3 <- c(NA, NA, NA, NA, NA)
year.4 <- c(NA, NA, NA, NA, NA)
year.5 <- c(NA, NA, NA, NA, NA)
df <- as.data.frame(cbind(rel, year.in, year.out, year.1, year.2, year.3,
year.4, year.5))
What I would like to do is update the missing values in year.1 - year.5 with the value of 'rel', but only if: (year.in >= year.i AND year.out <= year.i) (with i is 1:5)
Focussing on the just the year of entry, I came up with this:
for (i in 1:5) ifelse(df$year.in < i,
df[paste("year", i, sep= ".")]<- NA,
df[paste("year", i, sep= ".")]<- df["rel"])
But this merely replaces all year.i variables with the value of rel.
I have two questions:
how can I update the year.i variables with the 'rel' values on the conditions mentioned?
is it bad to use the if else statement here?
Best and thanks in advance,
Richard