[R] recode() function results in logical output, not factor output
Chuck Cleland
ccleland at optonline.net
Tue Jan 8 10:50:43 CET 2008
On 1/7/2008 3:32 PM, Thomas MacFarland wrote:
> Dear R Users:
>
> I have race-ethnicity groups identified in the factor variable Ethnic_G.
>
> I need to collapse Ethnic_G into a new variable with only two factors, 1 (White, non-Hispanic) and 2 (Minority).
>
> As seen in the code and output below, the recoded race-ethnicity variable is put into logical format, not factor format.
>
> I've used library(car) and the package was updated.
>
> Any ideas on how to fix this problem would be apprecidated.
In your call to recode(), use 'W' rather than 'c(W)' to specify the
White-non-Hispanic category:
> recode(Ethnic_G.Character, "'W'=1; else=2", as.factor.result=TRUE)
[1] 2 2 2 2 1 1 2 1 2 1 2 1 1 2 2 2 2 1 2 2 1 2 2
[24] 2 2
Levels: 1 2
OR
> recode(Ethnic_G.Character, "'W'='White-non-Hispanic';
else='Minority'", as.factor.result=TRUE)
[1] Minority Minority
[3] Minority Minority
[5] White-non-Hispanic White-non-Hispanic
[7] Minority White-non-Hispanic
[9] Minority White-non-Hispanic
[11] Minority White-non-Hispanic
[13] White-non-Hispanic Minority
[15] Minority Minority
[17] Minority White-non-Hispanic
[19] Minority Minority
[21] White-non-Hispanic Minority
[23] Minority Minority
[25] Minority
Levels: Minority White-non-Hispanic
Also, you can recode the factor directly without converting it to
character:
> recode(Ethnic_G, "'W'='White-non-Hispanic'; else='Minority'",
as.factor.result=TRUE)
[1] Minority Minority
[3] Minority Minority
[5] White-non-Hispanic White-non-Hispanic
[7] Minority White-non-Hispanic
[9] Minority White-non-Hispanic
[11] Minority White-non-Hispanic
[13] White-non-Hispanic Minority
[15] Minority Minority
[17] Minority White-non-Hispanic
[19] Minority Minority
[21] White-non-Hispanic Minority
[23] Minority Minority
[25] Minority
Levels: Minority White-non-Hispanic
> Thanks in advance!
>
> Best wishes.
>
> Tom
>> #####################################################################> # Task - recode Ethnic_G into two breakout groups, White-non-Hispanic> # and Other> class(Ethnic_G)[1] "factor"> print(Ethnic_G) [1] B A I U W W B W H W N W W B A B H W A H W B I N ILevels: A B H I N U W> Ethnic_G.Character <- as.character(Ethnic_G)> class(Ethnic_G.Character)[1] "character"> print(Ethnic_G.Character) [1] "B" "A" "I" "U" "W" "W" "B" "W" "H" "W" "N" "W" "W" "B" "A" "B" "H" "W" "A" "H" "W" "B" "I" "N" "I"> Ethnic_G.Recode <- recode(Ethnic_G.Character, "c(W)='1'; else='2'",+ as.factor.result=TRUE)> class(Ethnic_G.Recode)[1] "logical"> print(Ethnic_G.Recode) [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA> #####################################################################
>
> _________________________________________________________________
> Watch “Cause Effect,” a show about real people making a real difference.
>
> [[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.
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894
More information about the R-help
mailing list