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
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
Hi,
May be this helps:
set.seed(1)
dat1<-data.frame(B20_C1=c(NA,sample(0:1,4,replace=TRUE),NA),B20_C2=c(NA,sample(0:1,3,replace=TRUE),sample(0:1,2,replace=TRUE)),nrB20C=c(1,NA,NA,NA,NA,NA))
dat1
#? B20_C1 B20_C2 nrB20C
#1???? NA???? NA????? 1
#2????? 0????? 0???? NA
#3????? 0????? 1???? NA
#4????? 0????? 0???? NA
#5????? 1????? 1???? NA
#6???? NA????? 1???? NA
?dat1$pharm<-ifelse(apply(dat1,1,function(x) all(is.na(x[1:2]))),"no response",ifelse(dat1[,1]==1,"Yes","No"))
dat2<-within(dat1,{pharm<-factor(pharm)})
?levels(dat2$pharm)
#[1] "No"????????? "no response" "Yes"???????
?dat2
#? B20_C1 B20_C2 nrB20C?????? pharm
#1???? NA???? NA????? 1 no response
#2????? 0????? 0???? NA????????? No
#3????? 0????? 1???? NA????????? No
#4????? 0????? 0???? NA????????? No
#5????? 1????? 1???? NA???????? Yes
#6???? NA????? 1???? NA??????? <NA>
table(dat2$pharm)
#???????? No no response???????? Yes
?#???????? 3?????????? 1?????????? 1
A.K.
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, October 24, 2012 5:17 AM
Subject: [R] Recode function car package erases previous values
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
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.
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Wednesday, October 24, 2012 3:31 PM
To: Pancho Mulongeni
Cc: R help
Subject: Re: [R] Recode function car package erases previous values
Hi,
May be this helps:
set.seed(1)
dat1<-data.frame(B20_C1=c(NA,sample(0:1,4,replace=TRUE),NA),B20_C2=c(NA,sample(0:1,3,replace=TRUE),sample(0:1,2,replace=TRUE)),nrB20C=c(1,NA,NA,NA,NA,NA))
dat1
#? B20_C1 B20_C2 nrB20C
#1???? NA???? NA????? 1
#2????? 0????? 0???? NA
#3????? 0????? 1???? NA
#4????? 0????? 0???? NA
#5????? 1????? 1???? NA
#6???? NA????? 1???? NA
?dat1$pharm<-ifelse(apply(dat1,1,function(x) all(is.na(x[1:2]))),"no response",ifelse(dat1[,1]==1,"Yes","No"))
dat2<-within(dat1,{pharm<-factor(pharm)})
?levels(dat2$pharm)
#[1] "No"????????? "no response" "Yes"
?dat2
#? B20_C1 B20_C2 nrB20C?????? pharm
#1???? NA???? NA????? 1 no response
#2????? 0????? 0???? NA????????? No
#3????? 0????? 1???? NA????????? No
#4????? 0????? 0???? NA????????? No
#5????? 1????? 1???? NA???????? Yes
#6???? NA????? 1???? NA??????? <NA>
table(dat2$pharm)
#???????? No no response???????? Yes
?#???????? 3?????????? 1?????????? 1
A.K.
Thank you, once I understand what you did I will use it, for now, I use a workaround
1. First I use the recode function
pharm<-as.factor(recode(B20_C1,"1='Yes';0='No'"))
2. Now I just subset
pharm[nr.B20C==1]<-'no resp'
and this gives me the desired output
No Yes no resp
716 7 6
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, October 24, 2012 5:17 AM
Subject: [R] Recode function car package erases previous values
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
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.
Hi Pancho,
I tried ur method:
pharm<-as.factor(recode(dat1$B20_C1,"1='Yes';0='No'"))
?pharm
#[1] <NA> No?? No?? Yes? Yes? <NA>
#Levels: No Yes
pharm[dat1$nrB20C==1]<-'no resp'
#Warning message:
#In `[<-.factor`(`*tmp*`, dat1$nr.B20C == 1, value = "no resp") :
?# invalid factor level, NAs generated
?pharm
#[1] <NA> No?? No?? Yes? Yes? <NA>
#Levels: No Yes
Not sure how you got the result.
A.K.
?
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, October 24, 2012 9:51 AM
Subject: RE: [R] Recode function car package erases previous values
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Wednesday, October 24, 2012 3:31 PM
To: Pancho Mulongeni
Cc: R help
Subject: Re: [R] Recode function car package erases previous values
Hi,
May be this helps:
set.seed(1)
dat1<-data.frame(B20_C1=c(NA,sample(0:1,4,replace=TRUE),NA),B20_C2=c(NA,sample(0:1,3,replace=TRUE),sample(0:1,2,replace=TRUE)),nrB20C=c(1,NA,NA,NA,NA,NA))
dat1
#? B20_C1 B20_C2 nrB20C
#1???? NA???? NA????? 1
#2????? 0????? 0???? NA
#3????? 0????? 1???? NA
#4????? 0????? 0???? NA
#5????? 1????? 1???? NA
#6???? NA????? 1???? NA
?dat1$pharm<-ifelse(apply(dat1,1,function(x) all(is.na(x[1:2]))),"no response",ifelse(dat1[,1]==1,"Yes","No"))
dat2<-within(dat1,{pharm<-factor(pharm)})
?levels(dat2$pharm)
#[1] "No"????????? "no response" "Yes"
?dat2
#? B20_C1 B20_C2 nrB20C?????? pharm
#1???? NA???? NA????? 1 no response
#2????? 0????? 0???? NA????????? No
#3????? 0????? 1???? NA????????? No
#4????? 0????? 0???? NA????????? No
#5????? 1????? 1???? NA???????? Yes
#6???? NA????? 1???? NA??????? <NA>
table(dat2$pharm)
#???????? No no response???????? Yes
?#???????? 3?????????? 1?????????? 1
A.K.
Thank you, once I understand what you did I will use it, for now, I use a workaround
1. First I use the recode function
pharm<-as.factor(recode(B20_C1,"1='Yes';0='No'"))
2. Now I just subset
pharm[nr.B20C==1]<-'no resp'
and this gives me the desired output
? ? No? ? Yes no resp
? ? 716? ? ? 7? ? ? 6
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, October 24, 2012 5:17 AM
Subject: [R] Recode function car package erases previous values
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
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.
Hi Arun,
I also used to get that error, but what class is your nr.B20C, mine is not a factor, it is numeric and perhaps that's why it works
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Wednesday, October 24, 2012 4:35 PM
To: Pancho Mulongeni
Cc: R help
Subject: Re: [R] Recode function car package erases previous values
Hi Pancho,
I tried ur method:
pharm<-as.factor(recode(dat1$B20_C1,"1='Yes';0='No'"))
?pharm
#[1] <NA> No?? No?? Yes? Yes? <NA>
#Levels: No Yes
pharm[dat1$nrB20C==1]<-'no resp'
#Warning message:
#In `[<-.factor`(`*tmp*`, dat1$nr.B20C == 1, value = "no resp") :
?# invalid factor level, NAs generated
?pharm
#[1] <NA> No?? No?? Yes? Yes? <NA>
#Levels: No Yes
Not sure how you got the result.
A.K.
?
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, October 24, 2012 9:51 AM
Subject: RE: [R] Recode function car package erases previous values
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Wednesday, October 24, 2012 3:31 PM
To: Pancho Mulongeni
Cc: R help
Subject: Re: [R] Recode function car package erases previous values
Hi,
May be this helps:
set.seed(1)
dat1<-data.frame(B20_C1=c(NA,sample(0:1,4,replace=TRUE),NA),B20_C2=c(NA,sample(0:1,3,replace=TRUE),sample(0:1,2,replace=TRUE)),nrB20C=c(1,NA,NA,NA,NA,NA))
dat1
#? B20_C1 B20_C2 nrB20C
#1???? NA???? NA????? 1
#2????? 0????? 0???? NA
#3????? 0????? 1???? NA
#4????? 0????? 0???? NA
#5????? 1????? 1???? NA
#6???? NA????? 1???? NA
?dat1$pharm<-ifelse(apply(dat1,1,function(x) all(is.na(x[1:2]))),"no response",ifelse(dat1[,1]==1,"Yes","No"))
dat2<-within(dat1,{pharm<-factor(pharm)})
?levels(dat2$pharm)
#[1] "No"????????? "no response" "Yes"
?dat2
#? B20_C1 B20_C2 nrB20C?????? pharm
#1???? NA???? NA????? 1 no response
#2????? 0????? 0???? NA????????? No
#3????? 0????? 1???? NA????????? No
#4????? 0????? 0???? NA????????? No
#5????? 1????? 1???? NA???????? Yes
#6???? NA????? 1???? NA??????? <NA>
table(dat2$pharm)
#???????? No no response???????? Yes
?#???????? 3?????????? 1?????????? 1
A.K.
Thank you, once I understand what you did I will use it, for now, I use a workaround 1. First I use the recode function
pharm<-as.factor(recode(B20_C1,"1='Yes';0='No'"))
2. Now I just subset
pharm[nr.B20C==1]<-'no resp'
and this gives me the desired output
? ? No? ? Yes no resp
? ? 716? ? ? 7? ? ? 6
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, October 24, 2012 5:17 AM
Subject: [R] Recode function car package erases previous values
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
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.
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
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
Hi,
I think the as.factor in ur code created trouble.
library(car)
pharm<-as.factor(recode(B20_C1,"1='Yes';0='No'"))
????????????? ^^^^^^^^
pharm<-recode(dat1$B20_C1,"1='Yes';0='No'")
?pharm[dat1$nrB20C==1]<-"no resp"
pharm
#[1] "no resp" "No"????? "No"????? "Yes"???? "Yes"???? NA??????
?table(pharm)
#pharm
? # ? No no resp???? Yes
??? #? 2?????? 1?????? 2
A.K.
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, October 24, 2012 10:41 AM
Subject: RE: [R] Recode function car package erases previous values
Hi Arun,
I also used to get that error, but what class is your nr.B20C, mine is not a factor, it is numeric and perhaps that's why it works
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Wednesday, October 24, 2012 4:35 PM
To: Pancho Mulongeni
Cc: R help
Subject: Re: [R] Recode function car package erases previous values
Hi Pancho,
I tried ur method:
pharm<-as.factor(recode(dat1$B20_C1,"1='Yes';0='No'"))
?pharm
#[1] <NA> No?? No?? Yes? Yes? <NA>
#Levels: No Yes
pharm[dat1$nrB20C==1]<-'no resp'
#Warning message:
#In `[<-.factor`(`*tmp*`, dat1$nr.B20C == 1, value = "no resp") :
?# invalid factor level, NAs generated
?pharm
#[1] <NA> No?? No?? Yes? Yes? <NA>
#Levels: No Yes
Not sure how you got the result.
A.K.
?
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, October 24, 2012 9:51 AM
Subject: RE: [R] Recode function car package erases previous values
-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com]
Sent: Wednesday, October 24, 2012 3:31 PM
To: Pancho Mulongeni
Cc: R help
Subject: Re: [R] Recode function car package erases previous values
Hi,
May be this helps:
set.seed(1)
dat1<-data.frame(B20_C1=c(NA,sample(0:1,4,replace=TRUE),NA),B20_C2=c(NA,sample(0:1,3,replace=TRUE),sample(0:1,2,replace=TRUE)),nrB20C=c(1,NA,NA,NA,NA,NA))
dat1
#? B20_C1 B20_C2 nrB20C
#1???? NA???? NA????? 1
#2????? 0????? 0???? NA
#3????? 0????? 1???? NA
#4????? 0????? 0???? NA
#5????? 1????? 1???? NA
#6???? NA????? 1???? NA
?dat1$pharm<-ifelse(apply(dat1,1,function(x) all(is.na(x[1:2]))),"no response",ifelse(dat1[,1]==1,"Yes","No"))
dat2<-within(dat1,{pharm<-factor(pharm)})
?levels(dat2$pharm)
#[1] "No"????????? "no response" "Yes"
?dat2
#? B20_C1 B20_C2 nrB20C?????? pharm
#1???? NA???? NA????? 1 no response
#2????? 0????? 0???? NA????????? No
#3????? 0????? 1???? NA????????? No
#4????? 0????? 0???? NA????????? No
#5????? 1????? 1???? NA???????? Yes
#6???? NA????? 1???? NA??????? <NA>
table(dat2$pharm)
#???????? No no response???????? Yes
?#???????? 3?????????? 1?????????? 1
A.K.
Thank you, once I understand what you did I will use it, for now, I use a workaround 1. First I use the recode function
pharm<-as.factor(recode(B20_C1,"1='Yes';0='No'"))
2. Now I just subset
pharm[nr.B20C==1]<-'no resp'
and this gives me the desired output
? ?? No? ?? Yes no resp
? ? 716? ? ?? 7? ? ?? 6
----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Wednesday, October 24, 2012 5:17 AM
Subject: [R] Recode function car package erases previous values
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
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.