Appending output to a vector in R
1
0
Entering edit mode
3.6 years ago
vinayjrao ▴ 250

Hi,

I am performing some calulations in a script, and would like to run a t-test on the outputs of both conditions. I would like to know if there is a way for me to tell R to run the script 'n' number of times (without a loop), and if I can append the output of the conditions (Control vs Treatment) into two vectors called control and treatment, whose lengths would be equal to the number of times the calculation was run. Below is an example of what I wish to do.

control = geneA %in% control_tpm

control = length(control[control == TRUE])

treatment = geneA %in% treatment_tpm

treatment = length(treatment[treatment == TRUE])

When n = 100

control = c(100 outputs of control)

treatment = c(100 outputs of treatment)

Thanks,

Vinay

Edit: I found this post, but it has to be run as a separate script, I would like to run it multiple times in the same script

R • 629 views
ADD COMMENT
1
Entering edit mode
3.6 years ago

Hi,

You can set the size of the vector and use indexing to index and change the vector. Or you can use the function append(), to append a value to a vector. In any case you need to iterate over a sequence.

n <- 100

ctrl <- c(rep(NA, n)) # a vector with NAs across 100 positions 
trt <- c(rep(NA, n)) # a vector with NAs across 100 positions 

for ( i in seq(n) ) {

   ctrl[i] <- i # add 1, 2, 3 .... in position 1, 2, 3 .....

   trt[i] <- i # add 1, 2, 3 .... in position 1, 2, 3 .....
}

If the vector does not have a predefined length you can use the append function, like:

n <- 100

ctrl <- c() 
trt <- c() 

for ( i in seq(n) ) {

   if ( n < 50) {
        ctrl <- append(ctrl, i) # add 1, 2, 3 .... 49
   } else {
        trt <- append(trt, i) # add 50, 51, 52 .... 100
   }

}

You need to define conditions, but it is not clear to me from your description.

António

ADD COMMENT

Login before adding your answer.

Traffic: 1540 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6