This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R loop for question

I need some help figuring out how to make a loop in R.

I have a data frame named x x dimensions are 12773 rows and 86 columns.

column 1 has gene name, columns 2-85 counts for my different libraries, and 86 column is the mean of the library counts in columns 2-85 for each gene name.

I need help making a loop that would create a data frame or matrix output with the values of residuals of mean:

count - mean for each library and gene.

Expected output would be a new data frame y with column 1 gene names from data frame x which is column 1. In addition to having columns 2-85 with values that correspond to count-mean

Data frame x:

gene accepted_hits_x1.bam   accepted_hits_x2.bam   ...   mean
AARS2      12                   6                             6

DATA FRAME: y

         gene       accepted_hits_x1.bam      accepted_hits_x1.bam   ....  
 1      AARS1          -6                            0
r

2 answers

No for Loops!

x[,2:85] - x[,86]

Yet how to add the gene column from x to this data frame

Just detailing Michael Dondrup answer:

y <- x[,2:85] - x[,86]
y <- cbind( x[,1], y )

Thank you! exactly what I was going to ask! :) about cbind

Thanks guys!

Log in to answer this question.