I want to fill values into a multiple dimension array in R. I know how to implement them in a loop. See below
a <- array(seq(1, 8), dim = rep(2, 3))
Case 1
b <- a
for (i in seq(length = dim(a)[2]))
{
for (j in seq(length = dim(a)[3]))
{
b[,i,j] <- b[,1,1]
}
}
i.e - fill first column of first stratum in all other columns:
#, , 1
#
# [,1] [,2]
#[1,] 1 1
#[2,] 2 2
#
#, , 2
#
# [,1] [,2]
#[1,] 1 1
#[2,] 2 2
Case 2
b <- a
for (i in seq(length = dim(a)[2]))
{
b[,i,] <- b[,1,]
}
i.e. fill first column of each strata in remaining columns of the strata:
#, , 1
#
# [,1] [,2]
#[1,] 1 1
#[2,] 2 2
#
#, , 2
#
# [,1] [,2]
#[1,] 5 5
#[2,] 6 6
Case 3
b <- a
for (i in seq(length = dim(a)[3]))
{
b[,,i] <- b[,,1]
}
i.e. fill first strata contents into all other strata:
#, , 1
#
# [,1] [,2]
#[1,] 1 3
#[2,] 2 4
#
#, , 2
#
# [,1] [,2]
#[1,] 1 3
#[2,] 2 4
How could I vectorize it? Thanks for any suggestions.