[R] str() options

Thompson, David (MNR) David.John.Thompson at ontario.ca
Thu Nov 29 17:35:54 CET 2007


Beautiful!

Exactly what I was looking for.

Thank you, Gabor
>-----Original Message-----
>From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] 
>Sent: November 27, 2007 05:02 PM
>To: Thompson, David (MNR)
>Subject: Re: [R] str() options
>
>On Nov 27, 2007 1:55 PM, Thompson, David (MNR)
><David.John.Thompson at ontario.ca> wrote:
>> Hello,
>>
>> Is there a way to have str() and ls.str() return all factor levels?
>>
>
>str is an S3 generic so you can define your own str.factor.  Here
>we show the usual str and then show it again after we have
>added our own str.factor:
>
>> str(iris)
>'data.frame':   150 obs. of  5 variables:
> $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
> $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
> $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
> $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
> $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1
>1 1 1 1 1 1 ...
>>
>> str.factor <- function(object, ...) {
>+ cat("factor w/", nlevels(object), "levels", levels(object), "\n")
>+ }
>> str(iris)
>'data.frame':   150 obs. of  5 variables:
> $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
> $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
> $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
> $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
> $ Species     :factor w/ 3 levels setosa versicolor virginica
>>
>
*************************************
Silviculture Data Analyst
Ontario Forest Research Institute
Ontario Ministry of Natural Resources
david.john.thompson at ontario.ca
http://ofri.mnr.gov.on.ca



More information about the R-help mailing list