mardi 7 décembre 2021

Conditional looping in R like in Stata

I need to loop through the sequence of variables and create a newvar conditional on other variables. The logic is the following:

loop through values i = 09 11 12 13 14 15 15b 15 16b 17 18 19
tempvar = "prefix" + "i" + "_variable"
newvar = 1 IF newvar != 1 AND tempvar == 1 AND condition1 == 2 AND condition2 == i

I tried the ifelse logic, but it does not give me the numbers I expect. It seems there are some issues with NAs and I do understand how it works.

Example of code I tried is below. It gives weird numbers, probably because there are many NA's in tempvar. Maybe I should use other approach instead of ifelse?

varnum <- c(paste0(sprintf("%02d", 9)), paste0(11:15), paste0('15b'), paste(16), paste0('16b'), paste0(17:19))
df$newvar <- NA
for (i in varnum) {
    tempvar <- df[[paste0('name',sprintf("%02s", i),'_variable)]]
    df$newvar <- ifelse(df$newvar != 1 & tempvar == "1" & df$condition1 == "2" & df$condition2 == i, 1, 0)
  }
table(df$newvar)

In Stata I would write it like that:

global varnum "09 11 12 13 14 15 15b 16 16b 17 18 19"
gen newvar = .
foreach i in $varnum {
        local tempvar = "name" + "`i'" + "_variable"
        replace newvar = 1 if `newvar' != 1 & `tempvar' == "1" & condition1 == "2" & condition2 == `i'
}
tab newvar

Due to restrictions I cannot share the actual data.

Aucun commentaire:

Enregistrer un commentaire