1

I am using R to code simulations for a research project I am conducting in college. After creating relevant data structures and generating data, I seek to randomly modify a proportion P of observations (in increments of 0.02) in a 20 x 20 matrix by some effect K. In order to randomly determine the observations to be modified, I sample a number of integers equal to P*400 twice to represent row (rRow) and column (rCol) indices. In order to guarantee that no observation will be modified more than once, I perform this algorithm:

  1. I create a matrix, alrdyModded, that is 20 x 20 and initialized to 0s.

  2. I take the first value in rRow and rCol, and check whether alrdyModded[rRow[1]][rCol[1]]==1; WHILE alrdyModded[rRow[1]][rCol[1]]==1, i randomly select new integers for the indices until it ==0

  3. When alrdyModded[rRow[1]][rCol[1]]==0, modify the value in a treatment matrix with same indices and change alrdyModded[rRow[1]][rCol[1]] to 1

  4. Repeat for the entire length of rRow and rCol vectors

I believe a good method to perform this operation is a while loop nested in a for loop. However, when I enter the code below into R, I receive the following error code:

R CODE:

propModded<-1.0
trtSize<-2
numModded<-propModded*400

trt1<- matrix(rnorm(400,0,1),nrow = 20, ncol = 20)
cont<- matrix(rnorm(400,0,1),nrow = 20, ncol = 20)
alrdyModded1<- matrix(0, nrow = 20, ncol = 20)

## data structures for computation have been intitialized and filled

rCol<-sample.int(20,numModded,replace = TRUE)
rRow<-sample.int(20,numModded,replace = TRUE)

## indices for modifying observations have been generated

for(b in 1:numModded){

    while(alrdyModded1[rRow[b]][rCol[b]]==1){
        rRow[b]<-sample.int(20,1)
        rCol[b]<-sample.int(20,1)}

trt1[rRow[b]][rCol[b]]<-'+'(trt1[rRow[b]][rCol[b]],trtSize)
    alrdyModded[rRow[b]][rCol[b]]<-1    

}

## algorithm for guaranteeing no observation in trt1 is modified more than once

R OUTPUT " Error in while (alrdyModded1[rRow[b]][rCol[b]] == 1) { : missing value where TRUE/FALSE needed "

When I take out the for loop and run the code, the while loop evaluates the statement just fine, which implies an issue with accessing the correct values from the rRow and rCol vectors. I would appreciate any help in resolving this problem.

1 Answer 1

1

It appears you're not indexing right within the matrix. Instead of having a condition like while(alrdyModded1[rRow[b]][rCol[b]]==1){, it should read like this: while(alrdyModded1[rRow[b], rCol[b]]==1){. Matrices are indexed like this: matrix[1, 1], and it looks like you're forgetting your commas. The for-loop should be something closer to this:

for(b in 1:numModded){

  while(alrdyModded1[rRow[b], rCol[b]]==1){
    rRow[b]<-sample.int(20,1)
    rCol[b]<-sample.int(20,1)}

  trt1[rRow[b], rCol[b]]<-'+'(trt1[rRow[b], rCol[b]],trtSize)
  alrdyModded1[rRow[b], rCol[b]]<-1    

}

On a side note, why not make alrdyModded1 a boolean matrix (populated with just TRUE and FALSE values) with alrdyModded1<- matrix(FALSE, nrow = 20, ncol = 20) in line 7, and have the condition be just while(alrdyModded1[rRow[b], rCol[b]]){ instead?

Sign up to request clarification or add additional context in comments.

1 Comment

It appears I confused my indexing with the Java language. Thanks for your recommendation, that does appear to be the better route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.