[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fwd: Re: [R] reduce levels]



Somebody was asking about how to change a whole dataset or collection of variables into factors without doing it one at a time. Here's an answer from r-help

--
Paul E. Johnson                       email: pauljohn_AT_ku.edu
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
1541 Lilac Lane, Rm 504
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700
--- Begin Message ---
On Tue, 8 Nov 2005 Bill.Venables_AT_csiro.au wrote:

> sub$mm <- factor(sub$mm)

Great. This is exactly what I need.

> is the simplest way to change a single factor in this way.  To do a
> whole data frame you just need a loop:
>
> drop_unused_levels <- function(data)
> 	as.data.frame(lapply(data, function(x) if(is.factor(x))
> factor(x) else x ))

Thanks for this additional information. Maybe I'll be in need of it
later.

Greetings,
Claus

______________________________________________
R-help_AT_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

--- End Message ---