[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@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.
Re: [R] recoding variables again :(
Hi David, Check ?%in% for a simpler approach. Regards, Jorge.- On Wed, Jan 30, 2013 at 8:42 PM, David Studer wrote: 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@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. [[alternative HTML version deleted]] __ 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.
Re: [R] recoding variables again :(
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 stude...@gmail.com To: r-help@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@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.
Re: [R] recoding variables again :(
dat1$VARNEW-rep(unknown,nrow(dat1)) dat1$VARNEW[dat1$RES_STA==X dat1$BIRTHPLACE %in% c(AG,AI,AR,BE,ZH)]-swiss --- On Wed, 30/1/13, arun smartpink...@yahoo.com wrote: From: arun smartpink...@yahoo.com Subject: Re: [R] recoding variables again :( To: stude...@gmail.com stude...@gmail.com Cc: R help r-help@r-project.org Date: Wednesday, 30 January, 2013, 5:06 PM 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 stude...@gmail.com To: r-help@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@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. [[alternative HTML version deleted]] __ 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.