Thanks. The trouble is that the output has to be based on index "indx = rank(modF)" .
• 0 views
•
link
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
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.
A brief introduction to “apply” in R
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, orapply. Using loops is clumsy and extremely slow compared to other options.In addition to all other comments: R is 1 indexed