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
}
}