[R] variable colnames

COMTE Guillaume g.comte at alliance-ir.net
Wed May 17 10:05:17 CEST 2006


Hy all,

I apologize i've used rownames instead of colnames in my first exemple. (that's why i changed the mail object) I was on mars when i wrote my question...

Every answer where correct ... but, because i've made a wrong question, people wern't able to understand...

So finally i'm speaking about colnames :

Let's be more precise:

I've got a query that gives me for a given serial number the names of errors that has occurs for this serial.

I've got a loop that retrieve data's from a database for each errors that has occurs.

In my loop i tag colnames with the name of the error, in order to access them later (when i've finished to get all data's) so the colnames are like this (i extract a table of 5 columns, the number of rows is variable and doesn't matter):
Nom_
Cpt_
Tfth_
Ftm_
Fts_
Just after the "_" i paste the error name (the error names are without spaces or numbers)

By example if the error name is "InletPressureToLow" the colnames will be 

"Nom_InletPressureToLow"
"Cpt_InletPressureToLow"
...

So i'm forging the colnames, and once i've done that i'm unable to access them again if i do like that (nomalarme1 contain errors names without number and spaces):

For (i in 1:length(nomalarme1))
nom_nom=paste("nom_",nomalarme1[i],sep="")
nom_cpt=paste("cpt_",nomalarme1[i],sep="")
nom_tfth=paste("tfth_",nomalarme1[i],sep="")
nom_ftm=paste("ftm_",nomalarme1[i],sep="")
nom_fts=paste("fts_",nomalarme1[i],sep="")

All of these vars contains valid colnames, but are view as character by R not a colnames, and i didn't find any way to cast it or else in order to make R show me the data's that are attached to the colname, or that R understand that it is not the characters that i'm looking for but the keyword that identify the name i've given to a column.

I've got another idea on how to do that (without using colnames has described in the answers), but i'm still interested if it exist a way to access colnames values by assigning to a var a colname name then using the var (the var contain the name of the column) to retieve the values... 

Thks all.


COMTE Guillaume




-----Message d'origine-----
De : Christoph Buser [mailto:buser at stat.math.ethz.ch] 
Envoyé : mardi 16 mai 2006 18:32
À : r-help at stat.math.ethz.ch
Cc : COMTE Guillaume
Objet : Re: [R] variable row names

Hi

I was confused by your example, too and still am a little bit
confused, so please excuse me if my example does not answer your 
question:

You have a data.frame "dat"
> dat <- data.frame(colname1 = 1:4, colname2 = 5:8)

You have a vector with the colnames of "dat"
> namen <- colnames(dat)

You want to acess on the values in dat bu using the vector "namen"
> for (i in namen)
>   print(dat[,i])

or

> for (i in 1:length(namen))
>   print(dat[,namen[i]])

Does that answer your question or is it more complicated?

Best regards,

Christoph

--------------------------------------------------------------
Christoph Buser <buser at stat.math.ethz.ch>
Seminar fuer Statistik, LEO C13
ETH Zurich	8092 Zurich	 SWITZERLAND
phone: x-41-44-632-4673		fax: 632-1228
http://stat.ethz.ch/~buser/
--------------------------------------------------------------


COMTE Guillaume writes:
 > Thks for your answer,
 > 
 > I wish to access colnames by using vars like in my example nom="toto" , if i type toto in R it will display the column named toto with the values that the column keep, i wish the same by typing nom, but in my case it shows the content of the var nom not the values of the column named toto, but "toto".
 > 
 > I can't be more accurate because i'm unable to formulate it an other way.
 > 
 > another example:
 > 
 > I've got a set of colnames :
 > 
 > > nomalarme1
 > [1] "AirFlowFailure"      "AirSensorFailure"    "GasLeek"
 > [4] "SuddenPressureDrop"  "InletPressureToLow"  "PressureRiseFailure"
 > [7] "HoseLeakTestFailure" "o50DCVoltageToLow"
 > 
 > All of these names identify colnames (like AirFlowFailure).
 > 
 > My problem is if i do 
 > 
 > For (i in 1:length(nomalarme1))
 > {
 > nomalarme1[i]
 > }
 > It will display the names of the columns not the datas, is there is something that can tell R to show data's that have for colname nomalarme[i], and not just the colname.
 > 
 > 
 > Thks for your answer, i've looked into S-Poetry and it seems that it isn't explained there, but my "poor" english maybe have missed it...
 > 
 > 
 > COMTE Guillaume
 > 
 > 
 >  
 > 
 > 
 > -----Message d'origine-----
 > De : Patrick Burns [mailto:pburns at pburns.seanet.com] 
 > Envoyé : mardi 16 mai 2006 17:12
 > À : COMTE Guillaume
 > Objet : Re: [R] variable row names
 > 
 > I don't understand your question, but perhaps the
 > subscripting section of chapter 1 of S Poetry can
 > help you.
 > 
 > 
 > Patrick Burns
 > patrick at burns-stat.com
 > +44 (0)20 8525 0696
 > http://www.burns-stat.com
 > (home of S Poetry and "A Guide for the Unwilling S User")
 > 
 > COMTE Guillaume wrote:
 > 
 > >Hy all,
 > >
 > > 
 > >
 > >I wish to use a variable as rownames for a set of datas.
 > >
 > > 
 > >
 > >By example :
 > >
 > > 
 > >
 > > 
 > >
 > >  
 > >
 > >>nom<-"toto"
 > >>    
 > >>
 > >
 > >  
 > >
 > >>prenom<-"tutu"
 > >>    
 > >>
 > >
 > >  
 > >
 > >>res<-c(1,2)
 > >>    
 > >>
 > >
 > >  
 > >
 > >>res<-t(res)
 > >>    
 > >>
 > >
 > >  
 > >
 > >>res
 > >>    
 > >>
 > >
 > >     [,1] [,2]
 > >
 > >[1,]    1    2
 > >
 > >  
 > >
 > >>colnames(res)<-c(nom,prenom)
 > >>    
 > >>
 > >
 > >  
 > >
 > >>res
 > >>    
 > >>
 > >
 > >     toto tutu
 > >
 > >[1,]    1    2
 > >
 > >  
 > >
 > >>nom
 > >>    
 > >>
 > >
 > >[1] "toto"
 > >
 > >  
 > >
 > >
 > > 
 > >
 > >I wish to call the rowname by the variable name, in this case it would
 > >be < nom > or < prenom > , but if i do that it gives me the name of the
 > >row not the values of it.
 > >
 > > 
 > >
 > >Is this possible or not?
 > >
 > > 
 > >
 > >Thks all.
 > >
 > > 
 > >
 > >COMTE Guillaume
 > >
 > > 
 > >
 > >
 > >	[[alternative HTML version deleted]]
 > >
 > >______________________________________________
 > >R-help at stat.math.ethz.ch mailing list
 > >https://stat.ethz.ch/mailman/listinfo/r-help
 > >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
 > >
 > >
 > >  
 > >
 > 
 > ______________________________________________
 > R-help at stat.math.ethz.ch mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-help
 > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list