[R] string problems in R

Sharpie chuck at sharpsteen.net
Wed Mar 24 19:46:15 CET 2010



Muting Zhang wrote:
> 
> Hello all
> 
> I have been working on my thesis using R. I am a newbie to R and met a
> problem
> that bothered me for a while due to my lack of acquaintance of R.
> 
> I am using R to query from SQL. I got a list of crsp_fundno of G-style
> mutual
> funds which is still alive. I use the following codes and got what I want:
> 
> library(RODBC)
> channel<-odbcConnect("CRSPFUND")
> g.crspfundno<-sqlQuery(channel,"select crsp_fundno from Fund_style where
> wbrger_obj_cd = 'G'order by crsp_fundno")
> g.crspfundno (got crsp_fundno of G-style fund from Fund_style table)
> y.crspfundno<-sqlQuery(channel,"select crsp_fundno from Fund_hdr where
> dead_flag
> = 'N'and end_dt=20091231 order by crsp_fundno")
> y.crspfundno (got crsp_fundno of still alive fund from Fund_hdr table)
> g$key<-paste(g.crspfundno$crsp_fundno)
> y$key<-paste(y.crspfundno$crsp_fundno)
> v.fundno<-intersect(g$key,y$key) (using intersect to get crsp_fundno of
> G-style
> mutual funds which is still alive.)
> v.fundno
> 
> What i need to do next is using the v.fundno I got to query from another
> table
> "Monthly_return" to get the mret coresponding to every v.fundno.
> I have only a basic idea of the code:
> for (i in 1:length(v.fundno)){
> gmret<-sqlQuery(channel,"select mret from Monthly_returns where
> crsp_fundno =
> toString(v.fundno[i])")
> }
> gmret
> R gave me an error with undefined function "toString"
> I know there should be some details added in to get it right,like define
> an
> object or how to convert number to string..
> 
> I would be appreciated if anyone gives me any clue about it.
> 
> Muting
> 
> 

I'm going to take a wild guess and say that the following might work:

gmret<-sqlQuery(channel,
  paste( "select mret from Monthly_returns where crsp_fundno =",
    v.fundno[i] )
)

If you could post example data, say the results of:

  head( v.fundno )

I could provide more than a wild guess.


Hope it helps!

-Charlie


-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://n4.nabble.com/string-problems-in-R-tp1685419p1685837.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list