Hello, I can't find how to get de column name from a data.frame dollar reference. To make it simple, I'd like to obtain "Bar" from a "foo$Bar" notation. I've tried col.names(foo$Bar), names(foo$Bar) and so on without sucess. Regards Blaise [[alternative HTML version deleted]]
Well, it's may not be the best way to do so, but you may use this: colnames(foo[n]) where n is the column number, or the column name inside '' ''. The $ can't be used because it turn the column content into a vector, loosing its "properties" (e.g.: column and row names). But when you using [], these data.frame properties are preserved, so you can extract them. Hope this helps. Rodrigo. 2010/5/3 adrien Penac <farfelu02@yahoo.fr>> Hello, > I can't find how to get de column name from a data.frame dollar reference. > > To make it simple, I'd like to obtain "Bar" from a "foo$Bar" notation. > I've tried col.names(foo$Bar), names(foo$Bar) and so on without sucess. > > Regards > > Blaise > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On 03-May-10 11:09:43, adrien Penac wrote:> Hello, > I can't find how to get de column name from a data.frame dollar > reference. > > To make it simple, I'd like to obtain "Bar" from a "foo$Bar" notation. > I've tried col.names(foo$Bar), names(foo$Bar) and so on without sucess. > > Regards > BlaiseYou cannot expect to! The reason is that foo$Bar is the object which is stored in list/dataframe foo *under* the name "Bar". "Bar" is the index, within foo, of that object. That object may be something that has a "names" attribute (such as a matrix with column names), and this it what would be returned by anything like names(foo$Bar); there is no reason to expect that such names should have any relationship with "Bar". However, I have to ask why you want to find the solution to such a question. In any code which has already extracted foo$Bar from foo, that code has done so by constructing the expression "foo$Bar", and so the code already knows the value "Bar" -- so look for it in the code! A possible exception to that secario is where you want to find out the name of a list/dataframe component in a particular position -- for example, you may want the name under which the 3rd compenent is indexed. For example: foo<-list(Bar1=1,Bar2=2,Bar3=3,Bar4=4) names(foo) # [1] "Bar1" "Bar2" "Bar3" "Bar4" names(foo)[3] # [1] "Bar3" Hoping this helps; if not, then more information is needed about why you are asking the question! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 03-May-10 Time: 12:36:35 ------------------------------ XFMail ------------------------------
> as.character(quote(foo$Bar)[[3]])[1] "Bar" Hint: this is nothing to do with data frames ($ applies to lists). $ is an operator, so foo$Bar is a call. quote() stops it being evaluated, [[3]] selects the third of the elements (which are $, foo, Bar) and as.character turns the name into a character string. On Mon, 3 May 2010, adrien Penac wrote:> Hello, > I can't find how to get de column name from a data.frame dollar reference. > > To make it simple, I'd like to obtain "Bar" from a "foo$Bar" notation. > I've tried col.names(foo$Bar), names(foo$Bar) and so on without sucess. > > Regards > > Blaise-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595