[R] Beginners question

Paul Johnson pauljohn at ku.edu
Fri Mar 19 14:31:23 CET 2004


I do this kind of thing all the time.  Here's an example program that 
iterates over files in a directory.  The files are called

DataCulture1
DataCulture2

and so forth.  I want to make a graph based on the first, then the 
second, and so forth.  This version will make a graph each time the 
function newgr() is run. This uses locator to make the user place labels 
on the graph. If you don't want it to interact that way, kill that line, 
or else the program will seem not to run.

run<-1
updateDataSet <- function(run)
  {
    aChar<-character(1)
    aChar<-as.character(run)
    dsname<-c(paste("DataCulture",aChar,sep=""))
    data<-read.table(dsname,header=T,as.is = TRUE)

  }

buildGraph <-function(data)
{
tmp1<-plot(data$acquaint~data$T,type='l', ylim=c(0,1),ylab="average 
proportion",xlab="PERIOD",lty=1,pch=1,main="")
par("new"=TRUE)
tmp2<-plot(data$harmony~data$T,type='l', ylim=c(0,1),ylab="average 
proportion",xlab="PERIOD",lty=2,pch=1,main="")
par("new"=TRUE)
tmp3<-plot(data$identical~data$T,type='l', ylim=c(0,1),ylab="average 
proportion",xlab="PERIOD",lty=3,pch=1,main="")
par("new"=TRUE)
tmp4<-plot(data$totalEntropy~data$T,type='l',ylim=c(0,1),ylab="",xlab="",lty=4,pch=1,main="", 
cex=10)

#want a legend?
#legend(max(data$T)/2,0.2,c("Acquaintance","Harmonious","Identical","Entropy"),lty=1:4,xjust=1,yjust=1)
#if you want to interact, do this:
#legend(locator(1),c("Acquaintance","Harmonious","Identical"),lty=1:3)
#or do this to place the lables, one after the other
text(locator(4),c("Acquaintance","Harmony","Identical","Entropy"))

}

redraw<-function()
  {
    dsname<-updateDataSet(run)
    buildGraph(dsname)
  }

newgr<-function()
{
  dsname<-updateDataSet(run)
  buildGraph(dsname)
  run <<- run+1
}

newgr()


Koen Hufkens wrote:

> Dear list,
>
> I've been messing around with coding functions in R and it just won't 
> make sense to me.
> Running my analysis by hand on command line is fine and works but 
> because of the repetitive nature of the job I would like to code a 
> function for it.
>
> My problem:
>
> I would like to read in data from a file in my current working dir.
>
> so my code would look like:
>
> myanalysis <- function(sample, samplenr){
> sample.samplenr <- read.geodata("sample.samplenr", arg, arg etc.)
> sample.samplenr.results <- someanalysis(sample.samplenr)
> }
>
> problem is that charater values aren't interpretated as they should. 
> Any tips and tricks? This should be fairly simple but I just can't 
> find any reference to it. Most input values in functions in R are 
> numeric or are strings to be interpretated with bolean statements but 
> aren't handled as input variable perse.
>
> Suggestions and comments would be greatly appreciated,
> Koen.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html



-- 
Paul E. Johnson                       email: pauljohn at ku.edu
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
1541 Lilac Lane, Rm 504                              
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700




More information about the R-help mailing list