[R] Conditional merging in R & if then statement

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Fri Aug 31 22:19:07 CEST 2012


1) just because you don't use sqldf doesn't make it better. merge only does the join, not the conditions, so you have to apply them after merge. Untested (you should provide a complete example):

result <- merge ( detail, pdetail, by=TDATE, suffixes=c(".a",".b") )
result <- subset ( result, STIM.a >= STIM.b and STIM.a <=MAXTIM.b ) 

You can add a select argument to subset to remove the pdetail columns.

2) if is different than ifelse ... read the Introduction to R document, and/or read the help pages on if and ifelse.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

ramoss <ramine.mossadegh at finra.org> 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 it only applies to the 1st row & 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;
>
>
>
>Thanks in advance for your help.
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Conditional-merging-in-R-if-then-statement-tp4641936.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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