[R] R-code help for filtering with for loop

John Antonydas Gaspar gasparj at uni-koeln.de
Mon Mar 2 11:30:59 CET 2009


Dear Sir / Madam,

I am new for R coding. Kindly help me out in sorting out the following problem.

There are 50 rows with six coloumns(you could see in the attached .txt file). I
wish to go for filtering this 50 rows for any one of the six coloumns
satisfying the value >= 64.

I need to have a final table with rows having >= 64 value in any one of the six
coloumns and the rest could be <=64. For this purpose I use the following R
code;
---------------
datax<-read.table("filter_test.txt",row.names=1,sep="\t",header=TRUE,dec =
".",as.is =TRUE,na.strings = "NA", colClasses = NA,check.names =
FALSE,strip.white = FALSE, blank.lines.skip = TRUE,
allowEscapes = FALSE, flush = FALSE,encoding = "unknown")


filter<-datax[,1:6]

filtered<-vector()

for(i in 1:(dim(filter)[1]))
{
for(j in 1:(dim(filter)[2]))
{
x=(filter[i,j])>=64
filtered[i]<-x
}
}

# summing the result of the above
sum(filtered)


which(filtered)
z<-which(filtered)
filereddata<-filter[z,]


write.table(filtereddata,file ="filterdgenes.txt",quote = TRUE, sep = "\t ",
            dec = ".",row.names=T,col.names = NA, qmethod = c (escape",
"double"))

---------------------------


There is something is missing in my coding therefore the filteration is done
according to the value of the last column that is the sixth coloumn value not
takiing into consideration the rest of the coloumns.

For example with the table in .txt file I have attached, the first coloumn has
29 rows having values >= 64 but the last coloumn has only 25 rows.

The filtered list should have around 29 rows but only 25 since the coding has
considered only the last coloumn.  How to sort out this problem. Kindly help me
out.

Thanking in advance,
With Regards,

antony


-- 
Thony
University of Cologne
50931 Cologne/Germany

Tel:  004922125918042
Handy:   004917683142627






More information about the R-help mailing list