Home > database >  R code error with nested loop and if statements
R code error with nested loop and if statements

Time:11-05

I have a nested loop using for which goes into a set of if statements. When my id column was sequential (1:6), the code worked fine. When I changed the id column contents to random numbers (1,5,9,11,15,21) the code no longer works and there is a reoccurring error "Error in if (data$death[i] < data$data[j]) { : missing value where TRUE/FALSE needed".

Here is the example of the new data frame and the code used to set up a vector and then to run the nested loop and if statements. If the id number is sequential 1:6, there are no errors. But when the id is changed to below, the error keeps coming up. Can anyone advise on why this error might be occurring and any suggestions on how to avoid it? Many thanks!

id|intervention|death
1|2|0
5|2|1
9|2|0
11|1|1
15|1|0
21|1|0
test <- c()

for (i in data$id[which(data$intervention == 1)]) {
  for(j in data$id[which(data$intervention == 2)]){
    if (data$death[i] < data$death[j]){
      test <- c(test,-1)}
    else if (data$death[i] > data$death[j]){
      test <- c(test,1)}
    else if(data$death[i]==data$death[j]){
      test <- c(test,0)}
  }
  test  
}        

CodePudding user response:

There could be easier ways to achieve your goal. This is one fix of the code.

data <- data.frame(
  id = c(1, 5, 6, 11, 15, 21),
  intervention = c(2, 2, 2, 1, 1, 1),
  death = c(0, 1, 0, 1, 0, 0)
)

test <- c()
for (i in data$id[which(data$intervention == 1)]) {
  print(paste0("id = ", i, ": "))
  for (j in data$id[which(data$intervention == 2)]) {
    if (data$death[data$id == i] < data$death[data$id == j]) {
      test <- c(test, -1)
    } else if (data$death[data$id == i] > data$death[data$id == j]) {
      test <- c(test, 1)
    } else if (data$death[data$id == i] == data$death[data$id == j]) {
      test <- c(test, 0)
    }
  }
  print(test)
  test <- c()
}

[1] "id = 11: "
[1] 1 0 1
[1] "id = 15: "
[1]  0 -1  0
[1] "id = 21: "
[1]  0 -1  0

  • Related