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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
2001-Aug-14 06:48 UTC
[R] Regular Expressions {was Processing all *.dat worked..}
>>>>> "PaulEJ" == Paul E Johnson <pauljohn at ukans.edu> writes:PaulEJ> I got great, quick advice here earlier today. One little hitch PaulEJ> was that the pattern in list.files wants a regular expression, PaulEJ> and *.dat was grabbing all files with .dat in them, not ones PaulEJ> ending in .dat. This code creates a *.dat.summary file with PaulEJ> the mean and std. deviation. of the numeric variables for every PaulEJ> *.dat file in the current directory. PaulEJ> myDat<-list.files(pattern="*.dat$") (many of us would say ``please use spaces around "<-" '' [recent versions of ESS do this automagically with the "_" key!]) PaulEJ> <.....> You are right: Pattern should be a *regular expression* However, it was ``pure luck'' that your "*.dat$" worked fine. Both "*" and "." don't mean what you think they mean; "." means ``any arbitrary character'' "*" means ``the previous [thing] arbitrary many (0,1,...) times'' where [thing] is a single character (or something more general for advanced usage) The correct pattern would have been pattern = "\\.dat$" which really is \.dat$ { the "\\" (doubling) is necessary to specify a single "\" in an R string} You can find a bit more about regular expression using R's help ?grep and ?apropos Regular Expressions (nicknamed "Regexp"s, "Regex"s or "RE"s) are a very powerful concept from the early years of Unix; probably first used for the "grep" (and "egrep") command; then, inside Emacs and lately Perl, Python, etc. All regexp implementations have quite a few things in common, but slightly differ when it becomes complicated. It's worth spending an hour or so learning the basics. A google search gives you tons of starting points. Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO D10 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._