Need Help Understanding A Programming Statment Written In R
1
0
Entering edit mode
11.1 years ago
robjohn7000 ▴ 110

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 • 2.7k views
ADD COMMENT
4
Entering edit mode
You can try these also: 
1) head(selData,100) 
2) selData[1:100,]
ADD REPLY
3
Entering edit mode
ADD REPLY
2
Entering edit mode

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.

ADD REPLY
2
Entering edit mode

In addition to all other comments: R is 1 indexed

ADD REPLY
2
Entering edit mode
11.1 years ago
  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)?
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 1594 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