[R] if then in R versus SAS

peter dalgaard pdalgd at gmail.com
Mon Aug 27 11:01:29 CEST 2012


On Aug 24, 2012, at 21:51 , Daniel Nordlund wrote:

>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On Behalf Of Marc Schwartz
>> Sent: Friday, August 24, 2012 12:06 PM
>> To: ramoss
>> Cc: r-help at r-project.org
>> Subject: Re: [R] if then in R versus SAS
>> 
>> 
>> On Aug 24, 2012, at 1:03 PM, ramoss <ramine.mossadegh at finra.org> wrote:
>> 
>>> I am new to R and I have the following SAS statements:
>>> 
>>> if otype='M' and ocond='1' and entry='a.Prop' then MOC=1;
>>> else MOC=0;
>>> 
>>> How would I translate that into R code?
>>> 
>>> Thanks in advance
>> 
>> 
>> 
>> See ?ifelse and ?Logic, both of which are covered in "An Introduction to
>> R" (http://cran.r-project.org/manuals.html).
>> 
>>  MOC <- ifelse((otype == 'M') & (ocond == '1') & (entry == 'a.Prop'), 1,
>> 0)
>> 
>> 
>> You might also want to think about getting a copy of:
>> 
>> R for SAS and SPSS Users
>> Robert Muenchen
>> http://www.amazon.com/SAS-SPSS-Users-Statistics-Computing/dp/0387094172
>> 
>> Regards,
>> 
>> Marc Schwartz
>> 
> 
> I would second Mark's recommendation to carefully work through "An Introduction to R" and to get Robert Muenchen's book.  If the variables otype, ocond, and entry are scalar values, then the translation from SAS to R is very straight-forward:
> 
> if(otype=='M' && ocond=='1' && entry=='a.Prop') MOC <- 1 else MOC <- 0

It's almost certain that they are not scalar though, so Marc's idea is likely right. 

Just let me add that ifelse() is not actually needed:

MOC <- as.numeric((otype == 'M') & (ocond == '1') & (entry == 'a.Prop'))

will do. (And the as.numeric bit is only to convert FALSE/TRUE to 0/1)



> 
> 
> Hope this is helpful,
> 
> Dan
> 
> Daniel Nordlund
> Bothell, WA USA
> 
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com




More information about the R-help mailing list