[R] while loop until end of file
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Mon Aug 30 06:27:59 CEST 2010
Eh?
## First calculate the means:
mns <- with(alldata, tapply(param1, list(Pair, group), mean))
## now put the results into a data frame
res <- data.frame(mns, dif = mns[, "D"] - mns[, "R"])
## Then write it out if that's your thing.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Marcel Curlin
Sent: Monday, 30 August 2010 2:08 PM
To: r-help at r-project.org
Subject: [R] while loop until end of file
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.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list