[R] recode() function results in logical output, not factor output

John Fox jfox at mcmaster.ca
Tue Jan 8 13:09:32 CET 2008


Dear Chuck and Thomas,

Thanks, Chuck, for answering Thomas's question.

A small point: If Thomas recode()s the factor directly, as you suggest, then as.factor.result=TRUE is the default.

Regards,
 John

--------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Chuck Cleland
> Sent: January-08-08 4:51 AM
> To: Thomas MacFarland
> Cc: r-help at r-project.org
> Subject: Re: [R] recode() function results in logical output, not
> factor output
> 
> 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
> 
> ______________________________________________
> 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.




More information about the R-help mailing list