Re: [R] Recoding variables in R

2018-05-23 Thread Lisa van der Burgh
Thank you all for your help, it worked!

Op 23 mei 2018 om 19:27 heeft marta azores 
mailto:martazo...@gmail.com>> het volgende geschreven:

Try that code

NewDF<-DF[!DF$Anxiolytics==1,]

2018-05-23 10:14 GMT+00:00 Lisa van der Burgh 
mailto:lisavdbu...@hotmail.com>>:
Hi all,


I have a very general question and I think for you maybe very easy, but I am 
not able to solve it.

I have a dataset and that dataset contains the variable Anxiolytics. This 
variable is coded as 0, 1, 2. The variable looks as follows:


> summary(DF$Anxiolytics)
   01 2   NA's
1102   0 20440


You can see that the variable is coded as 0, 1, 2, but group 1 is 'empty'. So I 
want to remove this group, such that I get:

> summary(DF$Anxiolytics)
   0  2   NA's
1102 20  440

And then I want to recode the variable in a way that 2 becomes 1, such as:

> summary(DF$Anxiolytics)
   0  1   NA's
1102 20  440

Can you help me with the code for doing this?

Thank you in advance!
Best, Lisa


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To 
UNSUBSCRIBE and more, see
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.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Recoding variables in R

2018-05-23 Thread Rui Barradas

Hello,

See if this inspires you.


set.seed(1962)
DF <- data.frame(Anxiolytics = factor(sample(c(0, 2, NA), 1000, TRUE), 
levels = 0:2))

summary(DF$Anxiolytics)

DF$Anxiolytics <- droplevels(DF$Anxiolytics)
summary(DF$Anxiolytics)

DF$Anxiolytics <- factor(DF$Anxiolytics, labels = 0:1)
summary(DF$Anxiolytics)


Hope this helps,

Rui Barradas

On 5/23/2018 11:14 AM, Lisa van der Burgh wrote:

Hi all,


I have a very general question and I think for you maybe very easy, but I am 
not able to solve it.

I have a dataset and that dataset contains the variable Anxiolytics. This 
variable is coded as 0, 1, 2. The variable looks as follows:



summary(DF$Anxiolytics)

01 2   NA's
1102   0 20440


You can see that the variable is coded as 0, 1, 2, but group 1 is 'empty'. So I 
want to remove this group, such that I get:


summary(DF$Anxiolytics)

0  2   NA's
1102 20  440

And then I want to recode the variable in a way that 2 becomes 1, such as:


summary(DF$Anxiolytics)

0  1   NA's
1102 20  440

Can you help me with the code for doing this?

Thank you in advance!
Best, Lisa


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Recoding variables in R

2018-05-23 Thread William Dunlap via R-help
It looks like your data has class "factor".  If you call factor() on a
factor variable, without supplying an explicit 'levels' argument it
produces a new factor variable without any levels not present in the input
factor.  E.g.,

> fOrig <- factor(c(NA,"A","B","D",NA,"D"), levels=c("D","C","B","A"))
> summary(fOrig)
   DCBA NA's
   20112
> fNew <- factor(fOrig)
> summary(fNew)
   DBA NA's
   2112

You can use levels(fNew) <- c("delta","beta","alpha") to give the levels
new names.

I think that renaming "0", "2", "3"  to "0","1","2" will lead to
confusion.  The levels
are just names, so give them names that mean something to a human, like
"control",
"streptomycin (2 mg/kg)", etc.


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, May 23, 2018 at 3:14 AM, Lisa van der Burgh  wrote:

> Hi all,
>
>
> I have a very general question and I think for you maybe very easy, but I
> am not able to solve it.
>
> I have a dataset and that dataset contains the variable Anxiolytics. This
> variable is coded as 0, 1, 2. The variable looks as follows:
>
>
> > summary(DF$Anxiolytics)
>01 2   NA's
> 1102   0 20440
>
>
> You can see that the variable is coded as 0, 1, 2, but group 1 is 'empty'.
> So I want to remove this group, such that I get:
>
> > summary(DF$Anxiolytics)
>0  2   NA's
> 1102 20  440
>
> And then I want to recode the variable in a way that 2 becomes 1, such as:
>
> > summary(DF$Anxiolytics)
>0  1   NA's
> 1102 20  440
>
> Can you help me with the code for doing this?
>
> Thank you in advance!
> Best, Lisa
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


[R] Recoding variables in R

2018-05-23 Thread Lisa van der Burgh
Hi all,


I have a very general question and I think for you maybe very easy, but I am 
not able to solve it.

I have a dataset and that dataset contains the variable Anxiolytics. This 
variable is coded as 0, 1, 2. The variable looks as follows:


> summary(DF$Anxiolytics)
   01 2   NA's
1102   0 20440


You can see that the variable is coded as 0, 1, 2, but group 1 is 'empty'. So I 
want to remove this group, such that I get:

> summary(DF$Anxiolytics)
   0  2   NA's
1102 20  440

And then I want to recode the variable in a way that 2 becomes 1, such as:

> summary(DF$Anxiolytics)
   0  1   NA's
1102 20  440

Can you help me with the code for doing this?

Thank you in advance!
Best, Lisa


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Recoding Variables in R

2010-01-28 Thread John Fox
Dear Abraham,

If I follow correctly what you want to do, the following should do it:

> f <- factor(c(1, 1, 5, 5, 8, 8, 9, 9, 0, 0))
> f
 [1] 1 1 5 5 8 8 9 9 0 0
Levels: 0 1 5 8 9
> recode(f, " '1'=3; '5'=1; '0'=2; else=NA ")
 [1] 331122   
Levels: 1 2 3

I think that your problem was that you didn't distinguish correctly between
factor levels and their numeric encoding; factor levels should be quoted in
recode().

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-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On
> Behalf Of Mathew, Abraham T
> Sent: January-28-10 10:15 AM
> To: r-help@r-project.org
> Subject: [R] Recoding Variables in R
> 
> 
> 
> VAR 980490
> 
> Some people have suggested placing new limits on foreign
> 
> imports in order to protect American jobs. Others say
> 
> that such limits would raise consumer prices and hurt
> 
> American exports.
> 
> Do you FAVOR or OPPOSE placing new limits on imports, or
> 
> haven't you thought much about this?
> 
> 1. Favor
> 
> 5. Oppose
> 
> 8. DK
> 
> 9. NA; RF
> 
> 0. Haven't thought much about this
> 
> 
> 
> 
> 
> I am trying to recode the data for the following public opinion question
from
> the ANES. I would like to throw out 8 and 9. Furthermore, I would like to
> reorder the responses so that:
> 
> 1. Oppose (originally 5)
> 
> 2. Haven't though much about this (originally 0)
> 
> 3. favor (originally 1)
> 
> 
> 
> I tried the following, which did not work:
> 
> library(car)
> data96$V961327 <- recode(data96$V961327, "c(1)=2; c(2)=3; c(3)=1")
> 
> 
> 
> 
> 
> I also tried the following, which also did not work:
> 
> new <- as.numeric(data96$V961327)
> new
> data96$V961327 <- recode(new, "c(5)=1; c(0)=2; c(1)=3")
> 
> 
> 
> 
> 
> 
> 
> Help,
> 
> Abraham M
> 
> 
> 
> __
> R-help@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.

__
R-help@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.


[R] Recoding Variables in R

2010-01-28 Thread Mathew, Abraham T
 

VAR 980490 

Some people have suggested placing new limits on foreign

imports in order to protect American jobs. Others say

that such limits would raise consumer prices and hurt

American exports.

Do you FAVOR or OPPOSE placing new limits on imports, or

haven't you thought much about this?

1. Favor

5. Oppose

8. DK

9. NA; RF

0. Haven't thought much about this

 

 

I am trying to recode the data for the following public opinion question from 
the ANES. I would like to throw out 8 and 9. Furthermore, I would like to 
reorder the responses so that:

1. Oppose (originally 5)

2. Haven't though much about this (originally 0)

3. favor (originally 1)

 

I tried the following, which did not work:

library(car)
data96$V961327 <- recode(data96$V961327, "c(1)=2; c(2)=3; c(3)=1")

 

 

I also tried the following, which also did not work:

new <- as.numeric(data96$V961327)
new
data96$V961327 <- recode(new, "c(5)=1; c(0)=2; c(1)=3")

 

 

 

Help,

Abraham M

 

__
R-help@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.