Home > Mobile >  For loop in R not working to update data set
For loop in R not working to update data set

Time:12-09

I am trying to apply this for loop across the given minimal dataset. Each row of the dataset is associated with a patient, with each patient having two rows of information. The aim is that the cohort of each patient is determined. Each iteration of the for loop should first assign the cohort value of 'all' to the patient's first row of information, then see if they fall into that category, using rbinom(), an if else statement is then used to decide if the second row of the patients data should be assigned the cohort of 'y>=moderate' or 'DELETE'. However when I run this it does not get past the first iteration of the loop.

I have tried changing the nrow(na.df) to seq(1, 40, 2) given that the even valued rows are all accounted for in the odd valued rows iterations.

na.df <- data.frame(
  patno = c(94, 94, 104, 104, 154, 154, 155, 155, 159, 159, 182, 182, 213, 213,
             214, 214, 216, 216, 219, 219, 240, 240, 241, 241, 312, 312, 320, 
             320, 330, 330, 403, 403, 417, 417, 483, 483, 507, 507, 521, 521),
  BINew = c(rep(NA, 40)),
  cohort = factor(c(rep(NA, 40)), levels = c('all', 'y>=moderate', 'DELETE'))         
) 

out <- vector()
count = 0

for(val in c(1:nrow(na.df))) {
      count = count   1 
      na.df$cohort[val] <- "all"
          out[count] <- rbinom(1, 1, 0.5)
          na.df$BINew[val] <- as.numeric(out[count])
    
      if(na.df$BINew[val] == "0"){
            count = count   1
            na.df$cohort[val   1] <- "y>=moderate"
            out[count] <- rbinom(1, 1, 0.5)
            na.df$BINew[val   1] <- as.numeric(out[count])
          } else {
            na.df$cohort[val   1] <- "DELETE"
      }
    return(na.df)
}

CodePudding user response:

The loop runs through if you remove the returnand set the counter 1:(nrow(na.df) - 1). I have also put a cat into the loop to help you with debugging:

for(val in 1:(nrow(na.df) - 1)) {
  cat(val, "\n")
  count <- count   1 
  na.df$cohort[val] <- "all"
  out[count] <- rbinom(1, 1, 0.5)
  na.df$BINew[val] <- as.numeric(out[count])
  
  if(na.df$BINew[val] == "0"){
    count <- count   1
    na.df$cohort[val   1] <- "y>=moderate"
    out[count] <- rbinom(1, 1, 0.5)
    na.df$BINew[val   1] <- as.numeric(out[count])
  } else {
    na.df$cohort[val   1] <- "DELETE"
  }
}
na.df

Note also, that the code may have also other issues, and some parts that can be simplified. So for example the last row cannot be changed because you use val 1 as index. It may also be possible to vectorize it.

  • Related