I have a small example matrix. I have four elements row vector. I am passing it into a while loop. I am getting "cannot coerce type closure to vector of any error when I try to print "a1" in the while loop.
set.seed(123)
r <- 5 # number of rows
c <- 4
a <- c(1, 0, 2, 0, 3, 0, 2, 0, 1, 0, 0, 0, 1, 0, 2, 0, 1, 3, 2, 0)
a1 <- matrix(a, nrow=r, ncol=c, byrow=T)
a2 <- list(apply(a1, 2, max)) # max number in a column
a2 <- apply(a1, 2, max) # max number in a column
a2sum <- sum(a2)+1
set.seed(123)
sam <- sample(1:18, sum(a4), replace=F)
mat <- matrix(0, r, a2sum)
i <- 1
j <- 1
maxcol <- j
set.seed(123)
while(i <= r){
print("a2")
print(a2[k])
while(j <= a2sum){
print("i")
print(i)
print("j")
print(j)
print("maxcol=")
print(maxcol[j])
if( a1[i,j] == 0){
print("a1")
print(a1[i,j])
mat[i,j] <- 0
print("mat")
print(mat[i,j])
if(maxcol[j] == a2[k]){
print("a2")
print(a2[k])
[1] "a2"
[1] 3
[1] "i"
[1] 1
[1] "j"
[1] 1
[1] "maxcol="
[1] 1
Error in as.vector(x, mode) :
cannot coerce type 'closure' to vector of type 'any'
I am not sure why does the error pop up. The final output is to use "sam" to replace "mat", depending on the maximum value per column in the matrix "a1". Thanks for your comments and help.
Aucun commentaire:
Enregistrer un commentaire