[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
Fri Feb 9 16:41:34 CET 2007


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



More information about the R-help mailing list