[R] Extracting data from SQL Server based on R parameters

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Mon Jan 23 09:08:42 CET 2012

Without write access to the SQL database, you cannot merge data from both sources within the SQL query. You can use paste to build a list of literal strings that you can refer to in the SQL where clause with IN, pull the results into R, then use the merge function to combine the data frames in R.

Build the IN clause something like

literals <- paste ("'", mMPILatestHosp$ID,"'")
inclause <- paste ("ID IN (", paste(literals, collapse =","),")", sep="")

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.

dthomas <dyfed.thomas at midlandshn.health.nz> wrote:

>Can you extract data from a SQL server table based on parameters
>already in
>R? For example I have a list of unique patient IDs with latest
>hospitalisation date which I've processed in R. I need to join this
>data to
>other tables in SQL server. The tables in SQL server are too large to
>without reducing the number of records. I do not have write access to
>SQL server.
>mMPILatestHosp	#this is my  data frame of patients with unique IDs (ID)
>most recent date (EVENDate)
>The select query is easy
>query<-"select * (plus columns in mMPILatestHosp)  from Pharm2011
>where....#This is where I want to insert mMPILatestHosp$ID in the where
>I'm new to R so would appreciate your help.
>View this message in context:
>Sent from the R help mailing list archive at Nabble.com.
>R-help at r-project.org mailing list
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list