[R] str() options

jim holtman jholtman at gmail.com
Tue Nov 27 23:40:41 CET 2007


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?



More information about the R-help mailing list