[R] Concatenation and Evaluation

John Erb-Downward jre at umich.edu
Mon Feb 4 17:40:50 CET 2008


Hello all,
   I've run into what I bet is a silly problem; however, I've been  
trying to get around it now for a couple weeks and every time I think  
I have the answer it still doesn't work.  So I apologize in advance  
if this is painfully obvious, but I've run out of ideas and would  
really appreciate any input.

My situation is this, I'm importing a number of tab delimited text  
files that contain chromatogram data that I am trying to display  
adjacent to a dendrogram.  Since I look at multiple files I import  
them as file1, file2, file3, etc.  To display them in the order that  
comes from the dendrogram I want to use order.dendrogram, which gives  
me a vector of numbers that is the order which I want the  
chomatograms to be displayed.

Here's the problem:  I want to be able to set up a loop in which use  
"file" as the base name then concatenate the number that follows  
after it as the value at order.dendrogram[i] in a plot function.  So  
something like:

assume there is a dendrogram called peak.dend

numberOfChromatograms<-20
i<-1
ord<-order.dendrogram(peak.dend)

while(i<=numberOfChromatograms){
plot(file[ord[i])
i<-i+1
}

I know that this particular plot function won't work, but for the  
purposes of this assume that it would if I just put in the number as  
opposed to ord[i].  But when I try this I get an error that "file"  
does not exist.

So I thought that the problem was concatenation.  My solution was to  
use the paste function:

 > ord<-order.dendrogram(peak.dend)
 > ord
  [1] 13 14 17 18  7  8  1  2  3  4 15 16  5  6 23 24 19 20  9 10 11  
12 21 22
 > z<-paste("file",ord[1],sep="")
 > z
[1] "file13"

But when I try to plot this it won't work.  "Ah ha!" I thought, "it's  
the quotes."

so I try to unquote it:

 > noquote(z)
[1] file13

Yes!  But it still won't plot.  For some reason the statement file13  
is not getting evaluated.  If I type in exactly plot(file13) it  
works.  If I explicitly store file13 in z

 > z<-file13
then
 > plot(z)
it works.

But if I I do the following:

 > z<-noquote(paste("file",13,sep=""))
then
 > plot(z)

it will not plot (specifically it tells me that x and y are lengths  
differ which tells me that it's not evaluating the result of z, just  
inputting it as is).

I've naively tried eval(z), but I get the same result.

So I know this is a stupid problem and as soon as somebody tells me  
the answer I'm going to smack my head and say "Duh!", but right now I  
don't even know what terms to search for.  Thanks in advance for any  
help.

-John



More information about the R-help mailing list