I asked a similar question here: For loop conversion from stata to R, but want more detail and it was suggested that I start a new question.
I am trying to run a for loop and replicate what I know from Stata.
The data is structured like this:
PR1 PR2 ... PR25 PRDAY1 ...
"5123" "5022" ... "5451" 1 ...
"8542" "4023" ... "8595" 15 ...
"8543" "8541" ... "8595" 5 ...
"064" "" ... "" 2 ...
"064" "" ... "" 10 ...
"8542" "4023" ... "8595" 3 ...
Here is the stata code im trying to replicate:
generate newvar=0
forvalues i=1/25 {
replace newvar=1 if (PR`i'=="4576" & PRDAY`i'>-1 & PRDAY`i'<3) |
( PR`i'=="4573" & PRDAY`i'>-1 & PRDAY`i'<3)
}
And how I am trying to do it in R, but without success:
df$newvar <- 0
for (i in 1:25) {
df$newvar <- replace(df$newvar,
d[[paste0("PR", i)]] %in% c("4576", "4573") &
d[[paste0("PRDAY",i)]] %in% c(0,1,2),1)
}
Any thoughts or suggestions on how to improve this are greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire