This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Need Help Understanding A Programming Statment Written In R

Hi,

I have a data frame (selData) on which I'm using a for loop to collect the first 100 rownames and data into a samller data frame (exprs100.row) as follows:

data frame eaxmple:

            hr0       hr6      hr24      hr48      hr72      hr80
 1   0 0.9879942 0.9833003 0.9737801 0.9737969 0.9587732
 2   0 0.9928526 0.9860887 0.9812401 0.9840965 0.9737232
 3   0 0.9956888 1.0116334 1.0155620 1.0247329 1.0252338

for loop:

for(i in 0:99){
indx = rank(modF) == nrow(selData) - i
exprs.row = selDataMatrix_ave.log.ratio[indx,]
 exprs100.row[[i]] <- exprs.row[i]
 }

The statement that I have problem with is :

exprs100.row[[i]] <- exprs.row[i]

Could someone please help with the correct way of writing it. Thanks

r bioconductor microarray
You can try these also: 
1) head(selData,100) 
2) selData[1:100,]

please read ?'[' and read a basic introduction to R first. R is different from many other programming languages. As a rule of thumb in R you don't need to use for loops almost anywhere, but instead vectorized functions, sub setting, or apply. Using loops is clumsy and extremely slow compared to other options.

In addition to all other comments: R is 1 indexed

1 answer

  1. This is actually just an R usage question, it has nothing to do with bioinformatics.
  2. What exactly isn't working (i.e., what is the current output and what is the expected output)?
  3. If you just want to subset selDataMatrix_ave.log.ratio, wouldn't it be easier to just exprs100.row <- selDataMatrix_ave.log.ratio[c(1:100),] without a for loop (which is slow in R)?

Thanks. The trouble is that the output has to be based on index "indx = rank(modF)" .

Then something like

indx <- which(rank(modF) %in% c(nrow(selData):(nrow(selData)-99)))
exprs100.row <- selDataMatrix_ave.log.ratio[indx,]

without any loops or such is a better way of going. BTW, in the future, please say exactly what you're trying to do, that way we can give more appropriate feedback.

Log in to answer this question.