0

I have recently switched from STATA to R, and am stuck on some mechanisms of looping (I know looping is considered bad form here in lieu of writing a function, but my feeling is if I can figure out how to get a loop to do this, I can apply that to a function).

I regularly want to execute a call on nested variable name fragments. I cant get R to let me insert variable name fragments, either within a loop or a function; help! and while the variable name here is a number, often its a word or word fragment (a list of non-numeric, non-sequential characters, so i would like to be sure that whatever help i get isn't necessarily specific to inserting numbers!

here is an example. i would like to create an indicator variable for a patient having received a specific intervention in my dataframe called xdata. i have created several (8) intervention variables, and for each, a list of the specific patients (identified through the variable named id) who should be flagged as having received that intervention.

xdata <- data.frame(intervention1 = 0,
                intervention2 = 0,
                intervention3 = 0,
                intervention4 = 0,
                intervention5 = 0,
                intervention6 = 0,
                intervention7 = 0,
                intervention8 = 0,
                id = 1:2000)



ids <- list(dx1 = c(86, 1486, 1451), 
        dx2 = c(328, 1277,1458),
        dx3 = c(535,1569,689),
        dx4 = c(488,1335),
        dx5 = c(1210,1425,932,1451,30,270,347,418,709,801,1278),
        dx6 = c(282,721,749,1134),
        dx7 = c(932,43,148,158,1441),
        dx8 = c(932,801,1258))


for (i in 1:8) {
  for (j in 1:length(ids[[i]]) {
    parse(paste("xdata$intervention",i,"[xdata$id==",ids[[i]][j],"]<- 1"))
  }
}

What I am hoping for the loop to do is to carry out in sequence this line of code "xdata$intervention",i,"[xdata$id==",ids[[i]][j],"]<- 1" for each iteration of a particular intervention and list of patients, as applicable. I want the loop to do this:

    xdata$intervention1[xdata$id==86]<- 1
    xdata$intervention1[xdata$id==1486]<- 1
    xdata$intervention1[xdata$id==1451]<- 1
    xdata$intervention2[xdata$id==328]<- 1
    xdata$intervention2[xdata$id==1277]<- 1
    xdata$intervention2[xdata$id==1458]<- 1

...

    xdata$intervention8[xdata$id==932]<- 1
    xdata$intervention8[xdata$id==801]<- 1
    xdata$intervention8[xdata$id==1258]<- 1

Disclaimer/apology-- I have looked and looked for help with this in S.O., my guess is that the answer is out there, but one problem with being a newbie is that I have a hard time even knowing how to frame my question so the right answers will come up! If this is a duplicate I'm sorry; it was not for lack of trying that I didn't find it!

Babette
  • 1
  • 1
  • Look into the *apply family of functions (lapply, mapply) or dplyr package. I'm also trying to dynamically create variable names within a loop or apply, and then calculate values for those variables. – Mox Apr 05 '18 at 22:43
  • Check out here for some idea of how to use apply to get combinations of columns: https://stackoverflow.com/questions/45160809/calculating-correlation-between-columns-of-r-data-frame – Mox Apr 05 '18 at 22:46

0 Answers0