[R] how to collapse categories or re-categorize variables?
Ista Zahn
izahn at psych.rochester.edu
Fri Jul 16 21:19:54 CEST 2010
Hi,
On Fri, Jul 16, 2010 at 5:18 PM, CC <turtysmail at gmail.com> wrote:
> I am sure this is a very basic question:
>
> I have 600,000 categorical variables in a data.frame - each of which is
> classified as "0", "1", or "2"
>
> What I would like to do is collapse "1" and "2" and leave "0" by itself,
> such that after re-categorizing "0" = "0"; "1" = "1" and "2" = "1" --- in
> the end I only want "0" and "1" as categories for each of the variables.
Something like this should work
for (i in names(dat)) {
dat[, i] <- factor(dat[, i], levels = c("0", "1", "2"), labels =
c("0", "1", "1))
}
-Ista
>
> Also, if possible I would rather not create 600,000 new variables, if I can
> replace the existing variables with the new values that would be great!
>
> What would be the best way to do this?
>
> Thank you!
>
>
> --
> Thanks,
> CC
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
More information about the R-help
mailing list