[R] conditional recoding a variable

jim holtman jholtman at gmail.com
Thu Jul 2 13:19:32 CEST 2009


I think you want to use 'ifelse':

social$CitizenType<-ifelse(social$CitizenType==""
||social$PrimaryLanguage=="English","US Citizen" )

On Wed, Jul 1, 2009 at 5:45 PM, Chris Anderson<chris6764 at netzero.net> wrote:
> I have a variable that identifies citizen type but some of my rows are blank. I want to replace the blank entries with the correct value which is conditional on another variable. In SAS, my conditional statement would be as follow:
> if citizenType="" and primarylanguage="English" then citizenType ="US citizen"; else citizenType=citizenType;
> When I try to duplicate this conditional statement in R I get the following error: Will you show me the correct syntax?
> social$CitizenType<-if(social$CitizenType=="" ||social$PrimaryLanguage=="English","US Citizen" )
> Error: unexpected ',' in "social$CitizenType<-if(social$CitizenType=="" ||social$PrimaryLanguage=="English","
>
>  social$CitizenType<-if(social$CitizenType=="" && social$PrimaryLanguage=="English","US Citizen" )
> Error: unexpected ',' in "social$CitizenType<-if(social$CitizenType=="" && social$PrimaryLanguage=="English","
>
> social$CitizenType[social$CitizenType==""]<-if(social$CitizenType[social$CitizenType==""] && social$PrimaryLanguage[social$PrimaryLanguage=="English"],"US Citizen" )
> Error: unexpected ',' in "social$CitizenType[social$CitizenType==""]<-if(social$CitizenType[social$CitizenType==""] && social$PrimaryLanguage[social$PrimaryLanguage=="English"],"
>>
>
> Chris Anderson
> 707.315.8486
> www.sassydeals4u.com
> ____________________________________________________________
> Click now for prescreened plumbing contractors.
> http://thirdpartyoffers.netzero.net/TGL2241/fc/BLSrjpYePbmJll7hN4GC9GCio2T0Hpy2G8xkxYL2nS71xPBRcqvSBxjyBqQ/
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list