[R] recoding variables-recode not working

David Winsemius dwinsemius at comcast.net
Thu Apr 8 14:20:51 CEST 2010


On Apr 8, 2010, at 8:13 AM, Vlatka Matkovic Puljic wrote:

> Dear,
>
> my variable is numerical indicating how many times smb done test
> summary(Q12)
>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
> 0.0000  0.0000  0.0000  0.7989  1.0000 30.0000 66.0000
>
> I want to change this to categories--> 0=none testing; 1:30=done  
> testing (NA
> excluded)
>
> John, *cut(Q12, breaks=c(0,1,30), include.lowest=TRUE, labels=c('0',  
> '1 and
> more'))*
> is working.
> Only, when I want frequencies (for ex.) for this new 2 categories, R  
> is
> returning me freq for all 0 to 30.

It what fashion are you telling R that you "want frequencies"? Did you  
assign the output of cut(Q12, ...)  to a new variable?

-- 
David.
>
>
> 2010/4/7 John Fox <jfox at mcmaster.ca>
>
>> Dear Vlatka,
>>
>> It's impossible to know what the problem is without knowing  
>> something about
>> your data, which you didn't tell us either in this message or your
>> subsequent one.
>>
>> The recode command should work:
>>
>>> (x <- c(rep(0, 5), sample(1:30, 5, replace=TRUE)))
>> [1]  0  0  0  0  0 17 27 19 19  2
>>
>>> recode(x, "0='A'; 1:30='B'")
>> [1] "A" "A" "A" "A" "A" "B" "B" "B" "B" "B"
>>
>> The cut command requires include.lowest=TRUE and it helps to spell  
>> the
>> labels argument correctly:
>>
>>> cut(x, breaks=c(0,1,30), include.lowest=TRUE, labels=c('0', '1 and
>> more'))
>> [1] 0          0          0          0          0          1 and more
>> [7] 1 and more 1 and more 1 and more 1 and more
>> Levels: 0 1 and more
>>
>> I hope this helps,
>> John
>>
>> --------------------------------
>> John Fox
>> Senator William McMaster
>> Professor of Social Statistics
>> Department of Sociology
>> McMaster University
>> Hamilton, Ontario, Canada
>> web: 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 Vlatka Matkovic Puljic
>>> Sent: April-07-10 1:31 PM
>>> To: r-help at r-project.org
>>> Subject: [R] recoding variables-recode not working
>>>
>>> Hi,
>>>
>>> I have numerical variable that I want to recode into categories  
>>> '0' and
>> '1
>>> and more' and do analysis with that data.
>>> I have tried various of possibilities to do so, but I am sucked and
>> nothing
>>> is working.
>>>
>>> recode(Q12, "0='A';1:30='B'")
>>> cut(Q12, breaks=c(0,1,30), lables=c('0', '1 and more'))
>>> cat(Q12, "0=0;1-33=1")
>>>
>>> What should I do to make it right?
>>>
>>> --
>>> **************************
>>> Vlatka
>>>
>>>      [[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.
>>
>>
>>
>
>
> -- 
> **************************
> Vlatka Matković Puljić
> 095/8618 171
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list