[R] looping through a data frame

Ivan Baxter ibaxter at purdue.edu
Fri Jun 23 20:38:08 CEST 2006


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 at purdue.edu



More information about the R-help mailing list