0

My for loop looks like this:

for (i in 1:nrow(Base)) { 
  for (j in 3:ncol(Base)) { 
    if (Base[i, j] <= Base[i, 1]) {

      Base[i, 2] <- colnames(Base)[j]

      for (k in 3:ncol(Base)) {
        if (j == k) {
          Base[i+1, k] <- Base[i, 1] + hm("1.0")
        }
        else {
        Base[i+1, k] <- Base[i, k] 
        }
      }
    }
    i <- i + 1
  }
}

I would like that if the condition is verified for the first column, here 12, we fill the columns as mentioned then we pass to the next i and if necessary we verify the condition for the column 13, we fill the columns as mentioned before passing a next i and so on but my loop does not behave as I would like it from a certain iteration although the condition is well verified for the column 12.

I am probably doing it wrong and I would like to have your help please

Edit:

Base <- structure(list(tess = structure(c(1546302242, 1546307829, 1546312385, 
                                           1546312717, 1546315848, 1546330921, 1546331301, 1546334305, 1546334860, 
                                           1546336476), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                        taggs = c("", "", "", "", "", "", "", "", "", ""), a1 = structure(c(1546302242, 
                                                                                          NA, NA, NA, NA, NA, NA, NA, NA, NA), class = c("POSIXct", 
                                                                                                                                         "POSIXt"), tzone = "UTC"), a2 = structure(c(1546302242, NA, 
                                                                                                                                                                                     NA, NA, NA, NA, NA, NA, NA, NA), class = c("POSIXct", "POSIXt"
                                                                                                                                                                                     ), tzone = "UTC"), a3 = structure(c(1546302242, NA, NA, NA, 
                                                                                                                                                                                                                         NA, NA, NA, NA, NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                        a4 = structure(c(1546302242, NA, NA, NA, NA, NA, NA, NA, 
                                         NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                        a5 = structure(c(1546302242, NA, NA, NA, NA, NA, NA, NA, 
                                         NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                        a6 = structure(c(1546302242, NA, NA, NA, NA, NA, NA, NA, 
                                         NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                        a7 = structure(c(1546302242, NA, NA, NA, NA, NA, NA, NA, 
                                         NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
                                                                                                                 10L), class = "data.frame")

Edit 2:

for (j in 3:ncol(Base)) {
  for (i in 1:(nrow(Base)-1)) {
    if (Base[i, j] <= Base[i, 1] & (Base[i, 2] == "")) {
      Base[i, 2] <- colnames(Base)[j]
      
      Base[i+1, j] <- Base[i, 1] + hm("1.0")
    }
    else {
      
    Base[i+1, j] <- Base[i, j]
    }
  }
}

for (j in 3:ncol(Base)) {
  k = nrow(Base)
  if (Base[k, j] <= Base[k, 1]) {
    Base[k, 2] <- colnames(Base)[j]
    break
  }
}
user438383
  • 5,716
  • 8
  • 28
  • 43
ss10
  • 69
  • 6
  • 2
    It is easier, if you make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) or [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) with a sample input and your expected output. – Martin Gal May 01 '22 at 00:39
  • 3
    Why are you incrementing `i` inside a for loop? Also, you are assigning the `i+1`th row in your else clause while looping over 1:nrow(Base). The first line should at least be `for (i in 1:(nrow(Base)-1)) {`. – algae May 01 '22 at 00:50
  • @algae, thank you for your comments. I would like to check the condition in step 1 and increase by one hour the column that checks the condition while keeping the others intact and in iteration 2 I check the condition first for the column increased by one hour with the new value of column 1 before moving to the other columns and so on. – ss10 May 01 '22 at 01:24
  • @MartinGal, thank for your comment, I added a sample data – ss10 May 01 '22 at 01:26
  • How does you expected output look like? – Martin Gal May 01 '22 at 12:25
  • 2
    @MartinGal, I finally changed the structure of my loop and now everything works the way I want it to. Thank you very much for your help. – ss10 May 01 '22 at 13:28

0 Answers0