sbg
sbg

Reputation: 1772

Using a result to index a data.frame

I have a data.frame named pp that looks like this:

> head(pp)    
            X02R  X03N  X04K  X04N  X04R  X06I  X06N  X08J  X08P  X09O  X11O   
1961-02-28  42.0  43.0  96.2  10.2  13.5  42.4  21.1  79.1   2.7  15.2  14.3  
1962-02-28  17.1  22.7  27.9 140.7  10.0  36.7   8.2  47.2  28.5  31.5  24.0

Separately I am calculating

dmax<-douro[which(douro[,3]== max(douro[,3])),] 

which, for example gives me:

 > dmax   
   name catch corre.gauge  
31 X04K Douro  -0.5157648

Now I want to use the result dmax[1] (the name) to plot the respective column of data from pp. In this case I would like to plot pp$X04K. How can I do that in an automatic way (because I am going to repeat this process several times resulting in different columns from pp)?

Upvotes: 1

Views: 181

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70653

Joran just edited your post which means I will be asked to reload the page because an answer has been provided... Getting to the heart of the matter, you can extract the column name and pass it on to [ like so:

pp[, as.character(dmax[1,1])]

The comma is not mandatory but play around with it to see how the layout of the result changes.

Upvotes: 1

Related Questions