[R] while loop until end of file

Marcel Curlin cemarcel at u.washington.edu
Mon Aug 30 06:08:12 CEST 2010


Hi Guys,
stumped by a simple problem. I would like to take a file of the form

Pair     group     param1
1           D           10
1           D           10
1           R           10
1           D           10
2           D           10
2           D           10
2           D           10
2           R           10
2           R           10
etc..

and for each pair, calculate the average of param1 for group D entries,
subtract from the average of param1 for the group R entries, and then write
the results (ie, AveParam1D  AveParam1R dif) in a tab delimited file. Below
is the start of my code. the difficulty i am having is in creating a while
loop that stops once there are no more lines to read from the input file.
also not sure of the best way to write in the results, though I think I
should use rbind. 

data <- data.frame(alldata)

i <- 1
# need appropriate while loop
{
ss <- subset(data, Pair==i)
ssD <- subset(ss, DR==D)
ssR <- subset(ss, DR==R)
p1 <- mean(ssD$Length)
p2 <- mean(ssR$Length)
dif <- p1-p2
out <- rbind(data.frame(P1, P2, diff)
i <-i + 1
}

write.table(out, file="out", quote=F, row.names=F, col.names=T, sep="\t")

I have spent an absurd amount of time trying to sort this out with the
manual and forum searches. Any suggestions appreciated. 

Marcel

-- 
View this message in context: http://r.789695.n4.nabble.com/while-loop-until-end-of-file-tp2399544p2399544.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list