[R] looping through a data frame

From: Ivan Baxter <ibaxter_at_purdue.edu>
Date: Sat 24 Jun 2006 - 04:38:08 EST


Hi- I am having trouble with the syntax of looping through the rows and columns of a data frame.

I have a table with 17 observations for 84 lines at n=5-10 per line. So the table is ~700x17.

I want to pull out the median and stdev for each line and put it in a dataframe with rowname = linename.

So I have tried the following....
#read in the table

input.table <- read.table(file = "First_run_all.txt", header = T)
#pull out the line names

line.run <- unique(input.table$Line)
#pull out the column names except for Line
el.names <- names(input.table[2:18])

#now I want to calculate the median for each line for each column. The
code below would work for a matrix
calc.frame.med <- matrix(ncol = length(el.names), nrow = length(line.run), dimnames = list(line.run,el.names)) for(i in 1:length(el.names)){

    for(j in 1:length(line.run)){

       calc.frame.med[j,i] <- median(input.table[input.table$Line == line.run[j],el.names[i]])

    }
}

#however, it won't allow me to pull stuff out based on the row names
will it?
batch1.med <- calc.frame.med[rownames(calc.frame.med) == batch1,]
#doesn't work.
#It seems like I want to create the data as a matrix and then be able to
treat it like a data.frame.

can anyone set me straight on the right way to do this?

Thanks

Ivan

-- 
**************************************************************
Ivan Baxter
Research Scientist
Bindley Bioscience Center
Purdue University
765-543-7288
ibaxter@purdue.edu

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Jun 24 04:43:34 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 24 Jun 2006 - 08:14:38 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.