[R] Conditional Loop For Data Frame Columns

jawbonemurphy jdurlandster at gmail.com
Sun Jan 8 22:48:49 CET 2012


Hi,

I am trying to create a script that will evaluate each column of a data
frame, regardless of # columns, using some function and sorting the results
by an index vector:

#upload data (112 rows x 73 columns)
SD <- read.csv("/Users/johnjacob/Desktop/StudentsData_RInput.csv",
header=TRUE)

#assign index vector
ID <- SD[ ,2]

#write indexed mean function
meanfun <- function(x) {
for(i in 3:ncol(x)) {
  meanSD <- tapply(x[,i], ID, FUN=mean)}
return(meanSD)
}

#apply function to data
meanfun(SD)

What I get is one set of indexed means:

7605   Andrea    Billy   ERR006    FJM13 
2.111111 1.400000 1.888889 3.692308 3.750000 
   Gayan  Jschaef  Whitney 
1.300000 2.285714 2.000000 

...and what I would like to generate is a set of indexed means for each
column in the data set.  
Any guidance would be much appreciated!

Best,
Logan




--
View this message in context: http://r.789695.n4.nabble.com/Conditional-Loop-For-Data-Frame-Columns-tp4276821p4276821.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list