[R] Conditional merging in R & if then statement

David Winsemius dwinsemius at comcast.net
Fri Aug 31 22:48:20 CEST 2012


On Aug 31, 2012, at 10:11 AM, ramoss wrote:

> 1)I am wandering how the following SQL statement can be written in R language
> w/o using sqldf:
> create table  detail2 as
> select a.*
> from   detail a,
>       pdetail b
> where a.TDATE=b.TDATE
> and    (a.STIM >= b.STIM and a.STIM <=b.MAXTIM)
> 
> 2) when try if then in R

You need to provide code that shows what you did. If you had the output of 

dat <- merge(a, b, by=TDATE)

You could then limit your data depending on the names of the columns and whether MATIM was a duplicate name in "a" and "b" with something along the lines of

subset(dat, STIM.x >= STIM.y & STIM.s <=MAXTIM.y)


> it only applies to the 1st row

Yes, that is how "if" is used in R. You need to educate yourself about R rather than assuming the similar words are necessarily the same function. read:

?"if"
?ifelse


> & not to whole
> dataset like in SAS. How do you get round that?
> in SAS:
> 
> data summary;
>  set all1;
>  if entry='a:prop' then pctexec=stkful/stocks*100;
> run;
> 
-- 
David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list