[R] Recode function car package erases previous values

John Fox jfox at mcmaster.ca
Wed Oct 24 17:43:33 CEST 2012


Dear Pancho,

I'm not going to respond to the subsequent messages in this thread, since you appear to have solved your problem, just explain that what you did originally was to recode the variable B20_C1, creating the new variable pharm. Then you recoded another variable, nr.B20C, replacing the original version of pharm. I'm not sure why you expected this to give you what you want -- it really doesn't make sense.

Best,
 John

------------------------------------------------
John Fox
Sen. William McMaster Prof. of Social Statistics
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/

On Wed, 24 Oct 2012 09:17:25 +0000
 Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org> wrote:
> Hi all,
> I am attempting to create a new variable based on values of other variables. The variable is called pharm. It basically takes the numeric code of 1 as yes and 0 to be No from the variable B20_C1 (a question on a survey). However, I would also like to have a level for non-respondents and these are captured in the variable nr.B20C, which is a 1 when there is a non-response on the whole group of variable B20_C1 to B20_C5. So ultimately the variable pharmacy will have three levels, 
> Yes No and no resp.
> See below what happens
> 
> > pharm<-as.factor(recode(B20_C1,"1='Yes';0='No'"))
> > table(pharm)
> pharm
>  No Yes 
> 716   7 
> > levels(pharm)<-c('No','Yes','no resp')
> > table(pharm)
> pharm
>      No     Yes no resp 
>     716       7       0 
> > pharm<-as.factor(recode(nr.B20C,"1='no resp'"))
> > table(pharm)
> pharm
>       0 no resp 
>     723       6 
> >
> The recode variable just cannot seem to 'remember' I had just recoded 7 values to 'Yes' and 716 to be 'No'  and instead it assigns the level '0' which comes from nr.B20C (it has values 0 or 1). 
> This inconvenient as I would like to have ultimately the following table 
> pharm
>      No     Yes no resp 
>     716       7       6 (FROM nr.B20C where row has value 1).
> 
> Background. The variable pharm assess where you used the pharmacy to get your contraception.
> 
> Pancho Mulongeni
> Research Assistant
> PharmAccess Foundation
> 1 Fouché Street
> Windhoek West
> Windhoek
> Namibia
>  
> Tel:   +264 61 419 000
> Fax:  +264 61 419 001/2
> Mob: +264 81 4456 286
> 
> ______________________________________________
> 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