Re: [R] Recode factors

From: Abhijit Dasgupta <adasgupt_at_mail.jci.tju.edu>
Date: Thu, 27 Mar 2008 14:54:32 -0400

Another suggestion is:

blah = as.character(aa)
blah=gsub('[a-z]','0',blah,ignore.case=T) aa = as.factor(blah)

I've found changing factors to characters rather than numeric is generally safer.

Abhijit

Doran, Harold wrote:
> Perfect. My headache is gone. Thanks.
>
>
>> -----Original Message-----
>> From: Henrique Dallazuanna [mailto:wwwhsd_at_gmail.com]
>> Sent: Thursday, March 27, 2008 12:50 PM
>> To: Doran, Harold
>> Cc: r-help_at_r-project.org
>> Subject: Re: [R] Recode factors
>>
>> If I understand, you can try this:
>>
>> levels(x)[is.na(as.numeric(levels(x)))] <- 0
>>
>> On 27/03/2008, Doran, Harold <HDoran_at_air.org> wrote:
>>
>>> I know this comes up, but I didn't see my exact issue in
>>>
>> the archives.
>>
>>> I have variables in a dataframe that need to be recoded.
>>>
>> Here is what
>>
>>> I'm dealing with
>>>
>>> I have a factor called aa
>>>
>>> > class(aa)
>>> [1] "factor"
>>> > table(aa)
>>> aa
>>> * 0 1 2 3 A B C D L N T
>>> 0 0 1908 725 2089 0 0 67 0 0 2 1 6
>>>
>>> I need to recode everything that is not a numeric value
>>>
>> into a 0. So,
>>
>>> for example
>>>
>>> > mm <- ifelse(aa == 'B', 0, aa)
>>> > table(mm)
>>> mm
>>> 0 3 4 5 11 12 13
>>> 67 1908 725 2089 2 1 6
>>>
>>> The recoding works, but the values are no longer what they were
>>> previously. For example, what was a '1' is now a '4' etc.
>>>
>> Is there a
>>
>>> way to recode factors and also keep the values the same as
>>>
>> they were before?
>>
>>> That is, a '1' would remain a '1' after the recode?
>>>
>>> After the recoding, I need to convert to a numeric
>>>
>> variable. I can do
>>
>>> this as
>>>
>>> mm <- as.numeric(as.character(aa))
>>>
>>> Harold
>>>
>>>
>>> > sessionInfo()
>>> R version 2.6.2 (2008-02-08)
>>> i386-pc-mingw32
>>>
>>> locale:
>>> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
>>> States.1252;LC_MONETARY=English_United
>>> States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
>>>
>>> attached base packages:
>>> [1] stats graphics grDevices utils datasets
>>>
>> methods base
>>
>>> other attached packages:
>>> [1] gdata_2.4.0
>>>
>>> loaded via a namespace (and not attached):
>>> [1] gtools_2.4.0
>>> >
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>
>> --
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>>
>>
>
> ______________________________________________
> 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.
>

        [[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. Received on Fri 28 Mar 2008 - 16:15:02 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 Fri 28 Mar 2008 - 17:30:25 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.

list of date sections of archive