Anthony Staines
2007-Feb-09 15:41 UTC
[R] Using variable names in for loops - Generating plots semi-automatically from a series of variables Partly solved
Hi,
This code is trying to produce a series of graphics files, with plots
of male and female disease rates by age, one plot per disease. The
dataframe contains a variable 'Age' and a set of variables called
'Male_CVD, Female_CVD,Male_RTA,Female_RTA, and so on. 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)
--
This doesn't work, of course, because Male[i] is a character variable
which is the same as the name of a column in the dataframe, but it is
not the column itself, nor is it the name of the column.
> eval(expression(Male[1]))
[1] "Male_HEART"
So, how can I refer here to something in the dataframe?
This next code works, but by kind of bypassing my real question :-
--
Diseases <- c("Cardiovascular disease" ... ,"All causes")
Male <- names(data)[grep("Male",names(data))]
Female <- names(data)[grep("Female",names(data))]
for (i in seq(1,length(Diseases)))
{
jpeg(paste(Diseases[i],".jpg"))
plot(data[Male][[1]]~data$Age,type='b')
lines(data[Female][[i]]~data$Age,type='b')
dev.off()
}
--
Is this the R way to do it, or am I missing something more basic?
Anthony Staines
P.S. Using R 2.4.1 on Linux
--
Dr. Anthony Staines, Senior Lecturer in Epidemiology.
***Note New Address *** Note New Address ****
School of Public Health and Population Sciences, Woodview House,
UCD, Belfield, Dublin 4, Ireland.
Tel:- +353 1 716 7345. Fax:- +353 1 716 7407 Mobile:- +353 86 606 9713
Web:- http://phm.ucd.ie
Mark Lyman
2007-Feb-09 17:46 UTC
[R] Using variable names in for loops - Generating plots semi-automatically from a series of variables Partly solved
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