Why am I getting Error in ms[i, ] : subscript out of bounds ?

Hi, and welcome!

Thanks for posting the code. It is always helpful, though, to use a reproducible example, called a reprex.

Although df_All is easy enough to replicate by hand by cutting and pasting into a csv then

library(tidyverse) # interactive session
df_All <- read.csv("~/Desktop/dfAll.csv", header = TRUE)
df_All
#>    B1  B2   B3   B4   B5   B6   B7 class
#> 1 445 637  993 1197 2679 3387 2409     1
#> 2 525 636  948 1078 2613 3256 2517     1
#> 3 604 733 1049 1229 3377 3504 2891     1
#> 4 490 583  954 1359 3113 3742 2886     1
#> 5 216 292  535  620 2274 2327 1299     1

Created on 2019-11-18 by the reprex package (v0.3.0)

img and trainData_crs_utm aren't, and they are numeric values that are used in the cast to ms, and it's uncertain what a good guess would be. But I'll pick numbers out of a hat.

An aside: df is the name of a built in function:

df
#> function (x, df1, df2, ncp, log = FALSE) 
#> {
#>     if (missing(ncp)) 
#>         .Call(C_df, x, df1, df2, log)
#>     else .Call(C_dnf, x, df1, df2, ncp, log)
#> }
#> <bytecode: 0x7fad326c3758>
#> <environment: namespace:stats>

Created on 2019-11-18 by the reprex package (v0.3.0)

and it's good practice to avoid using it for an assignment result; my_df or even df. is preferable.

Aside: df <- round(dfAll) doesn't do anything.

library(tidyverse) # interactive session
df_All <- read.csv("~/Desktop/dfAll.csv", header = TRUE)
df_All == round(df_All)
#>        B1   B2   B3   B4   B5   B6   B7 class
#> [1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE  TRUE
#> [2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE  TRUE
#> [3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE  TRUE
#> [4,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE  TRUE
#> [5,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE  TRUE

On to the cast.

Created on 2019-11-18 by the reprex package (v0.3.0)

library(tidyverse) # interactive session
df_All <- read.csv("~/Desktop/dfAll.csv", header = TRUE)
ms <- matrix(NA, nrow = 7, ncol = 7)
ms
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,]   NA   NA   NA   NA   NA   NA   NA
#> [2,]   NA   NA   NA   NA   NA   NA   NA
#> [3,]   NA   NA   NA   NA   NA   NA   NA
#> [4,]   NA   NA   NA   NA   NA   NA   NA
#> [5,]   NA   NA   NA   NA   NA   NA   NA
#> [6,]   NA   NA   NA   NA   NA   NA   NA
#> [7,]   NA   NA   NA   NA   NA   NA   NA

Created on 2019-11-18 by the reprex package (v0.3.0)

It turns out that any nrow or ncol will have the same problem: all colMeans will be identically NA

library(tidyverse) # interactive session
df_All <- read.csv("~/Desktop/dfAll.csv", header = TRUE)
ms <- matrix(NA, nrow = 7, ncol = 7)
colMeans(ms)
#> [1] NA NA NA NA NA NA NA

Created on 2019-11-18 by the reprex package (v0.3.0)

Fix this and supply a reprex if you hit a bump again.

Finally, should this be homework, see the FAQ