Skip to content
Back to formatted view

Raw Message

Message-ID: <8F56F393-DA7F-4186-8684-33B23A559AEC@comcast.net>
Date: 2011-11-03T20:57:11Z
From: David Winsemius
Subject: Reclassify string values
In-Reply-To: <CA+hbrhVHEY3vaFK+eJN69JHW7EGJrQLVfXF5v6YMu+Lsb4kGmA@mail.gmail.com>

On Nov 3, 2011, at 4:40 PM, Peter Langfelder wrote:

> On Thu, Nov 3, 2011 at 1:31 PM, Zev Ross <zev at zevross.com> wrote:
>> Hi Peter,
>>
>> Thanks for the response. What you've suggested works fine but I'm  
>> looking
>> for something that is simpler than my solution and avoids the pesky  
>> warning
>> message. Your response avoids the warning message but just as  
>> complex (if
>> not more). I just assumed there would be a function along the lines  
>> of:
>>
>>> mydata <- c("A", "C", "A", "D", "B", "B")
>>> reclassify(mydata, inCategories=c("A", "B" ,"C", "D"),
>>>  outCategories=c("Group1", "Group1", "Group2", "Group2"))
>>
>> [1] "Group1" "Group2" "Group1" "Group2" "Group1" "Group1"
>>
>
> But of course, except sometimes you have to write the function  
> yourself.
>
> reclassify = function(data, inCategories, outCategories)
> {
>   outCategories[ match(data, inCategories)]
> }
>
> Sorry I can't make it any simpler than a 1-line solution :)

It will be difficult to beat a oneliner like that. If Zev is still  
holding out for a canned solution he might look in the 'car'' package  
where there is at least one function that does releveling and  
grouping. I foget its name at the moment but it wouldn't hurt a new  
learneR to scroll through the entire 'car' suite of functions.

-- 
David Winsemius, MD
Heritage Laboratories
West Hartford, CT