简体   繁体   中英

After For loop Error in .subset2(x, i, exact = exact) : subscript out of bounds

Table 1- KT

Quantity 2020-09-04 2020-09-05 2020-09-06 2020-09-07 2020-09-08
832         1          1          2          2          1
30          1          1          2          2          1
40          1          1          2          2          1
54          1          1          2          2          1
30          1          1          2          2          1

Table 2- FT

2020-09-03 2020-09-04 2020-09-05 2020-09-06 2020-09-07 2020-09-08
1600        NA         NA         NA         NA         NA
2000        NA         NA         NA         NA         NA
435         NA         NA         NA         NA         NA
568         NA         NA         NA         NA         NA
800         NA         NA         NA         NA         NA

for(i in seq_along(FT1)){
  FT1[i+1] <- FT1[,i] - (KT[c("Quantity")] * KT[[i+1]])
}

Output Table

2020-09-03 2020-09-04 2020-09-05 2020-09-06 2020-09-07 2020-09-08
 1600        768        -64        -1728      -3392      -4224
 2000        1970       1940        1880       1820       1790
 435         395        355         275        195        155
 568         514        460         352        244        190
 800         770        740         680        620        590

It Returns Output Table but with Error in .subset2(x, i, exact = exact) : subscript out of bounds

Got this Error After For Loop

> How to Remove this error

Here we need the index as

for(i in 2:ncol(FT)) FT[, i] <- FT[, i-1] - KT$Quantity * KT[[i]]

FT
#  2020-09-03 2020-09-04 2020-09-05 2020-09-06 2020-09-07 2020-09-08
#1       1600        768        -64      -1728      -3392      -4224
#2       2000       1970       1940       1880       1820       1790
#3        435        395        355        275        195        155
#4        568        514        460        352        244        190
#5        800        770        740        680        620        590

data

KT <- structure(list(Quantity = c(832L, 30L, 40L, 54L, 30L), `2020-09-04` = c(1L, 
1L, 1L, 1L, 1L), `2020-09-05` = c(1L, 1L, 1L, 1L, 1L), `2020-09-06` = c(2L, 
2L, 2L, 2L, 2L), `2020-09-07` = c(2L, 2L, 2L, 2L, 2L), `2020-09-08` = c(1L, 
1L, 1L, 1L, 1L)), class = "data.frame", 
row.names = c(NA, -5L
))

FT <- structure(list(`2020-09-03` = c(1600L, 2000L, 435L, 568L, 800L
), `2020-09-04` = c(NA, NA, NA, NA, NA), `2020-09-05` = c(NA, 
NA, NA, NA, NA), `2020-09-06` = c(NA, NA, NA, NA, NA), `2020-09-07` = c(NA, 
NA, NA, NA, NA), `2020-09-08` = c(NA, NA, NA, NA, NA)),
class = "data.frame", row.names = c(NA, 
-5L))

You can also try this modification on your loop:

#Loop
for(i in 1:(dim(FT1)[2]-1)){
  FT1[,i+1] <- FT1[,i] - (KT[c("Quantity")] * KT[[i+1]])
}

Which outputs:

  X2020.09.03 X2020.09.04 X2020.09.05 X2020.09.06 X2020.09.07 X2020.09.08
1        1600         768         -64       -1728       -3392       -4224
2        2000        1970        1940        1880        1820        1790
3         435         395         355         275         195         155
4         568         514         460         352         244         190
5         800         770         740         680         620         590

Some data used:

#Data 1
KT <- structure(list(Quantity = c(832L, 30L, 40L, 54L, 30L), X2020.09.04 = c(1L, 
1L, 1L, 1L, 1L), X2020.09.05 = c(1L, 1L, 1L, 1L, 1L), X2020.09.06 = c(2L, 
2L, 2L, 2L, 2L), X2020.09.07 = c(2L, 2L, 2L, 2L, 2L), X2020.09.08 = c(1L, 
1L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, -5L
))

#Data 2
FT1 <- structure(list(X2020.09.03 = c(1600L, 2000L, 435L, 568L, 800L
), X2020.09.04 = c(NA, NA, NA, NA, NA), X2020.09.05 = c(NA, NA, 
NA, NA, NA), X2020.09.06 = c(NA, NA, NA, NA, NA), X2020.09.07 = c(NA, 
NA, NA, NA, NA), X2020.09.08 = c(NA, NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, 
-5L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM