[R] paste command

Douglas Bates bates at stat.wisc.edu
Sun Nov 28 19:00:58 CET 2004


Doran, Harold wrote:
> In a previous post, I mentioned a loop being used to generate graphs. I have some sample code partially put together but have found one offending line of code that I cannot figure out what to do with.
>  
> I have one data frame called grade4. If I do something like
>  
> hist(grade4$math)
>  
> I get the appropriate chart.
>  
> Within the loop, however, I am doing this for multiple files and grades, so I use the paste command, but it isn't working. For example, the code would perform something similar to:
>  
> hist(paste("grade", "4", "$math",sep=""))
>  
> which doesn't work. The code below is what I hope will ultimately be the working loop.
>  
> grade.list<-as.vector(unique(egsingle$grade))
> for(grade.number in grade.list){
> pdf(paste("grade",grade.number,".pdf",sep=""))
> hist(paste("grade",grade.number,"$math",sep=""))
> dev.off()}
>  
> I'm a little confused on how to get this work properly. Any thoughts are much appreciated.

Well, you could read the section of the FAQ that Uwe quoted in an 
earlier response today :-)

A short version of that section of the FAQ is that if you use

get(paste("grade", "4", sep = ""))[["math"]]

then you can generalize the "4" and the "math" to variable names.




More information about the R-help mailing list