Re: [R] Refactor all factors in a data frame

From: hadley wickham <h.wickham_at_gmail.com>
Date: Tue, 05 Jun 2007 15:07:42 +0200

Hi Hilmar,

Try this:

cat <- sapply(df, is.factor)
df[cat] <- lapply(df[cat], factor)

Hadley

On 6/5/07, Hilmar Berger <hilmar.berger_at_imise.uni-leipzig.de> wrote:
> Hi all,
>
> Assume I have a data frame with numerical and factor variables that I
> got through merging various other data frames and subsetting the
> resulting data frame afterwards. The number levels of the factors seem
> to be the same as in the original data frames, probably because subset()
> calls [.factor without drop = TRUE (that's what I gather from scanning
> the mailing lists).
>
> I wonder if there is a easy way to refactor all factors in the data
> frame at once. I noted that fix(data_frame) does the trick, however,
> this needs user interaction, which I'd like to avoid. Subsequent
> write.table / read.table would be another option but I'm not sure if R
> can guess the factor/char/numeric-type correctly when reading the table.
>
> So, is there any way in drop the unused factor levels from *all* factors
> of a data frame without import/export ?
>
> Thanks in advance,
> Hilmar
>
> --
>
> Hilmar Berger
> Studienkoordinator
> Institut für medizinische Informatik, Statistik und Epidemiologie
> Universität Leipzig
> Härtelstr. 16-18
> D-04107 Leipzig
>
> Tel. +49 341 97 16 101
> Fax. +49 341 97 16 109
> email: hilmar.berger_at_imise.uni-leipzig.de
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>



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 and provide commented, minimal, self-contained, reproducible code. Received on Tue 05 Jun 2007 - 13:21:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 05 Jun 2007 - 14:31:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.