R: conditionally replace values in loop -


i have dataframe 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 update missing values in year.1 - year.5 value of 'rel', if: (year.in >= year.i , year.out <= year.i) (with 1:5)

focussing on year of entry, came 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 merely replaces year.i variables value of rel.

i have 2 questions:

  • how can update year.i variables 'rel' values on conditions mentioned?

  • is bad use if else statement here?

best , in advance,

richard

library(data.table) dt = data.table(df)  for(i in 1:5) dt[year.in <= & <= year.out, paste0('year.', i) := rel]  dt #   rel year.in year.out year.1 year.2 year.3 year.4 year.5 #1:   2       4        6     na     na     na      2      2 #2:   5      na        7     na     na     na     na     na #3:  na       2       na     na     na     na     na     na #4:   3       3        5     na     na      3      3      3 #5:   6       2        4     na      6      6      6     na 

Comments