Hello, Is there a way to have str() and ls.str() return all factor levels? Thanx, DaveT. ************************************* Silviculture Data Analyst Ontario Forest Research Institute Ontario Ministry of Natural Resources david.john.thompson at ontario.ca http://ofri.mnr.gov.on.ca
Does this do what you want?> x <- data.frame(a=factor(letters), b=1:26, c=factor(LETTERS)) > str(x)'data.frame': 26 obs. of 3 variables: $ a: Factor w/ 26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ... $ b: int 1 2 3 4 5 6 7 8 9 10 ... $ c: Factor w/ 26 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...> all.levels <- lapply(x, function(.col) if(is.factor(.col)) levels(.col) else NULL) > # output only non-NULL, which implies these are levels > all.levels[!sapply(all.levels, is.null)]$a [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" [26] "z" $c [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" [26] "Z" 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? > > Thanx, DaveT. > ************************************* > Silviculture Data Analyst > Ontario Forest Research Institute > Ontario Ministry of Natural Resources > david.john.thompson at ontario.ca > http://ofri.mnr.gov.on.ca > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
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