user2585431
user2585431

Reputation: 43

Populating matrix with vectors from 'for' loop - producing NAs

I'd like to populate rows of a matrix with vectors that I produce in a for loop. However, after I run the code, the matrix is still filled with NAs.

I am able to produce the vector "l" just fine - it's a vector of length n (6), but I can't seem to fill in "h" with the iterations of l.

Any help is greatly appreciated.

p_def = 0.50 
n = 6 
N = 10 
breaks = vector(length = n-1)

for (k in 1:N){
  h <- matrix(nrow=N,ncol=n)
for (i in 1:(n-1)){
  p = runif(1, 0, 1)
  if (p <= p_def){
      breaks[i] = 1
      }
  else {
      breaks[i]=0
      }
  }
temp = 1
j =1 
l = vector(length=n)
for (i in 1:n){
  if (breaks[i] == 1){
    l[j] = temp
    temp = 1
    j = 1 + j
    if (i ==n-1) {
      l[j]=temp}
  }
  if (breaks[i] == 0){
    temp = temp + 1
    if (i==n-1){
      l[j] = temp
      }
    }
}
h[k,] <- l
}
h

With Rodrigo's assistance, I edited the script to:

p_def = 0.50 # probability of defective
n = 6 # length of spore chains
N = 10 # number of spore chains
breaks = vector(length = n-1)
h <- matrix(nrow=N,ncol=n) 

for (k in 1:N){ 
  for (i in 1:(n-1)){
    p = runif(1, 0, 1)
    if (p <= p_def){
    breaks[i] = 1
  } else {
  breaks[i]=0
  }
}
temp = 1
j =1 
l = vector(length=n)
for (i in breaks){
  if (i == 1){
    l[j] = temp
    temp = 1
    j = 1 + j
    if (i ==n-1) {
      l[j]=temp}
  }
  if ([i == 0){
  temp = temp + 1
  if (i==n-1){
    l[j] = temp
  }
  }
}

h[k,] <- l
}
h
      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    2    2    2    0    0    0
 [2,]   NA   NA   NA   NA   NA   NA
 [3,]   NA   NA   NA   NA   NA   NA
 [4,]   NA   NA   NA   NA   NA   NA
 [5,]   NA   NA   NA   NA   NA   NA
 [6,]   NA   NA   NA   NA   NA   NA
 [7,]   NA   NA   NA   NA   NA   NA
 [8,]   NA   NA   NA   NA   NA   NA
 [9,]   NA   NA   NA   NA   NA   NA
[10,]   NA   NA   NA   NA   NA   NA

Upvotes: 1

Views: 124

Answers (1)

Rodrigo de Alexandre
Rodrigo de Alexandre

Reputation: 662

I don't have enough reputation to comment your post. Can show how the output should look like?

In the mean while I've seen some bugs in your code.

for (k in 1:N){
     h <- matrix(nrow=N,ncol=n)

Every time this loop starts it reset you h matrix.
Move h <- matrix(nrow=N,ncol=n) to outside the loop This is probably why you have NAs in every line.

Furthermore:

for (i in 1:n){
    if (breaks[i] == 1){
    ...
    }
    if (breaks[i] == 0){
    ...
    }

should be changed to:

for(i in breaks){
   if(i == 1){
   ...
   }
   if(i == 0){
   ...

you can't run a loop of interval n = 6 while breaks length = 5.
You are running a loop bigger than the vector itself... This will also generate NAs since it won't loop because the sizes are different

my h matrix output look like this when I fixed these points:

      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    1    1    3    0    0    0
 [2,]    1    1    0    0    0    0
 [3,]    1    1    1    1    0    0
 [4,]    2    2    1    0    0    0
 [5,]    2    2    1    0    0    0
 [6,]    4    1    0    0    0    0
 [7,]    4    1    0    0    0    0
 [8,]    1    1    1    0    0    0
 [9,]    3    1    1    0    0    0
[10,]    3    2    0    0    0    0

this is what I did:

p_def = 0.50 
n = 6 
N = 10 
breaks = vector(length = n-1)

h <- matrix(nrow=N,ncol=n)

for (k in 1:N){

    for (i in 1:(n-1)){
        p = runif(1, 0, 1)
        if (p <= p_def){
            breaks[i] = 1
        }
        else {
            breaks[i]=0
        }
    }
    temp = 1
    j =1 
    l = vector(length=n)
    for(i in breaks){
        if(i == 1){
            l[j] = temp
            temp = 1
            j = 1 + j
            if (i == n-1) {
                l[j]=temp}
        }
        if(i == 0){
            temp = temp + 1
            if (i==n-1){
                l[j] = temp
            }
        }
    }
    h[k,] <- l
}
h

Upvotes: 1

Related Questions