[R] recoding variables again :(

arun smartpink111 at yahoo.com
Wed Jan 30 12:36:37 CET 2013


Hi,


set.seed(125)
dat1<-data.frame(BIRTHPLACE=sample(c("AG","AI","AR","BE","ZH","USA","GER","ESP"),20,replace=TRUE),RES_STA=sample(LETTERS[c(1:3,24:25)],20,replace=TRUE))
dat1$VARNEW<-ifelse(dat1$RES_STA=="X" & dat1$BIRTHPLACE%in%c("AG","AI","AR","BE","ZH"),"swiss","unknown")
A.K.



----- Original Message -----
From: David Studer <studerov at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, January 30, 2013 4:42 AM
Subject: [R] recoding variables again :(

Hello everybody!

I have again a rather simple question concerning recoding of variables:

I have a variable/data-frame column BIRTHPLACE containing abbreviations of
the 26 swiss counties (AG, AI, AR, BE, ZH, ... )
as well as international country codes (USA, GER, ESP, etc.) and another
variable RES_STA indicating the
residence status (A, B, C, X, Y)

My goal is now to create a new variable VARNEW under the following
conditions:

   - should be the RESIDENCE_STATUS
   - except:
      - if RESIDENCE_STATUS is X and at the same time BIRTHPLACE is one of
      the 26
      swiss counties then it should be "swiss"
      - otherweise it should be "unknown"


I have already tried the following code:
mydata$VARNEW<-mydata$RESIDENCE_STATUS          # setting VARNEW as
RESIDENCE_STATUS

idx<-(mydata$RESIDENCE_STATUS=="X" && !(        # TRUE: unknown; FALSE:
swiss
mydata$BIRTHPLACE=="AG" |
mydata$BIRTHPLACE=="BE" |
mydata$BIRTHPLACE=="AR"
...
)
)

and then?

Thank you for any help!

David

    [[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