[R] recoding variables-recode not working

John Fox jfox at mcmaster.ca
Wed Apr 7 21:09:44 CEST 2010


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.



More information about the R-help mailing list