[R] Processing all *.dat worked. Thanks

Paul E Johnson pauljohn at ukans.edu
Tue Aug 14 00:45:13 CEST 2001


I got great, quick advice here earlier today.  One little hitch was that
the pattern in list.files wants a regular expression, and *.dat was
grabbing all files with .dat in them, not ones ending in .dat.  This
code creates a *.dat.summary file with the mean and std. deviation. of
the numeric variables for every *.dat file in the current directory.

myDat<-list.files(pattern="*.dat$")
createSummary <-function(dsName){
  data<-read.table(dsName,header=T,as.is = TRUE)
  indices<-1:dim(data)[2]
  indices<-na.omit(ifelse(indices*sapply(data,is.numeric),indices,NA))
  mean<-sapply(data[,indices],mean)
  sd<-sapply(data[,indices],sd)
  newOutput<-rbind(mean,sd)
  newOutput<-round(newOutput,digits=6)
  outputdsname<-c(paste(dsName,".summary",sep=""))
  write.table(t(newOutput),file=outputdsname,quote=FALSE,
sep="\t",col.names=FALSE)
}

processData <-function(dat){
  for (i in 1:length(dat))
  {
    createSummary(dat[[i]])
  }
}

processData(myDat)


-- 
Paul E. Johnson                       email: pauljohn at ukans.edu
Dept. of Political Science            http://lark.cc.ukans.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list