Anthony Staines <anthony.staines <at> gmail.com> writes:

For each
> disease, I want to pull out the column of data containing the word
> 'Male' and plot this against age, and then add a line to the plot for
> the corresponding column containing 'Female'.
> --
> attach(data)
> 
> Diseases <- c("Cardiovascular disease","Road Traffic Injury",  ...
> ,"All causes")
> Male <- names(data)[grep("Male",names(data))]
> Female <- names(data)[grep("Female",names(data))]
> #Disease contains disease labels in the correct order, and Male and
> Female now hold the (correct) variables.
> 
> for (i in seq(1,length(Diseases)))
>   {
> jpeg(paste(Diseases[i],".jpg")) #This works fine!
> plot(Male[i]~Age)                  #This does not
> lines(Female[i]~Age)
> dev.off()
> }
> detach(data)

[SNIP]

There are a few ways you can do this. Using plot.formula like you do here you 
can use this:

> mydata <- as.data.frame(matrix(runif(300),30,10))
> attach(mydata)
> plot(formula(paste(mynames[1],mynames[2],sep='~')))

A way to do this without a formula would be:
> plot(eval(parse(text=mynames[1])),eval(parse(text=mynames[2])))
> detach(mydata)

Take a look at the help files for eval and parse. I still do not have a firm 
grasp on how to use them and other related functions, like substitute, but 
what I have been able figure out has been very useful.

Mark Lyman

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to