[R] Re : Extract a 'data.frame$column' column name

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 3 14:17:40 CEST 2010


See the describe functions in these packages:

Hmisc
pysch
prettyR

On Mon, May 3, 2010 at 8:12 AM, adrien Penac <farfelu02 at yahoo.fr> wrote:
> Thank a lot for these answers.
>
> Some of you wondered why I needed
> to do that!
>
> In fact, I have not so big data.frame whith many
> columns (98) and many of them are similar (many binary answers, some
> factor data and a few quantitative datas).
>
> As I am a lazy guy, I
> wanted to do a sort of function that would apply some descriptive
> statistics (and maybe some graphics) to each on them regarding to its
> class.
> And I wanted the results to be outputed as :
> "Descriptive
> statistics for : Bar"
> results....
>
> then, this would allow me to do a :
>
> For (i in names(mydata)) desc(mydata[[i]])
>
> In
> fact, I was testing this function with a : desc(mydata$Bar)
> where
> desc was my lazy guy function defined as:
> desc <- function(x) {
>  print(paste("descriptive
> statistics for ",x))
>  if (is.factor(x)) {
>    print("Raw count:")
>    print(freq(x, total=T))
>    print("Net counts:")
>
> print(freq(x, total=T, exclude=NA))
>    }
>  else print(summary(x))
> }
>
> PS: freq is another function defined above
>
> Regards
>
>
>
> ________________________________
> De : Prof Brian Ripley <ripley at stats.ox.ac.uk>
>
> Cc : R-help at r-project.org
> Envoyé le : Lun 3 mai 2010, 13 h 40 min 15 s
> Objet : Re: [R] Extract a 'data.frame$column' column name
>
>> 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
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at 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.
>
>



More information about the R-help mailing list