It can help to apply a little abstraction, borrowing from school algebra.
f(x) = y
where
x is some object or object at hand
y is some object that is to be achieved
f is some function or function to transform x into y
Here
a <- matrix(0,35,8) # a matrix of zeros of dim 35,8
v <- c(0.01, 0.50, 0.09,0.4) # a vector of typeof numeric of length 4
I've abstracted away the name of the matrix from PopulationA
to just a
because what the matrix represents in terms of its relationship to the real world just doesn't matter. Likewise, for v
.
y will also be a matrix of equal dim
with a
. In it will be replaced all 0
values with values of v
"for every 2 columns"
a <- matrix(0,35,8)
v <- c(0.01, 0.50, 0.09, 0.4)
v <- rep(v, each = 2)
numbers <- 1:dim(a)[2]
vectors <- split(numbers, ceiling(seq_along(numbers) / 2))
runner <- vectors[1:4]
for(i in seq_along(runner)) a[,runner[i][[1]]] = v[runner[i][[1]]]
a |> head()
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#> [1,] 0.01 0.01 0.5 0.5 0.09 0.09 0.4 0.4
#> [2,] 0.01 0.01 0.5 0.5 0.09 0.09 0.4 0.4
#> [3,] 0.01 0.01 0.5 0.5 0.09 0.09 0.4 0.4
#> [4,] 0.01 0.01 0.5 0.5 0.09 0.09 0.4 0.4
#> [5,] 0.01 0.01 0.5 0.5 0.09 0.09 0.4 0.4
#> [6,] 0.01 0.01 0.5 0.5 0.09 0.09 0.4 0.4
Created on 2023-06-27 with reprex v2.0.2
Because the columns/variables are unnamed, the order in which the elements of v
are inserted into m
doesn't matter—they can be rearranged later. That is, instead of this column layout
#> [1] 0.01 0.01 0.5 0.5 0.09 0.09 0.4 0.4
we wanted
#> [1] 0.01 0.50 0.09 0.40 0.01 0.50 0.09 0.40
we could just
m <- m[,c(1,3,5,7,2,4,6,8]
or we could redefine how we construct v
v <- c(0.01, 0.50, 0.09, 0.4)
v <- c(v,v)
v
#> [1] 0.01 0.50 0.09 0.40 0.01 0.50 0.09 0.40
Also, if this is a one off, we can do numbers
more directly as
numbers = 1:8
By working both ends against the middle, we come up with the successive transforms that make up the abstract f. The key insight is that the original Infprob
vector needed to be expanded to match the number of columns of the original PopulationA
.