[R] String Matching

Glenn Schultz glennmschultz at me.com
Fri Jan 29 18:02:08 CET 2016


All,

I have a file named as so 313929BL4FNMA2432.rds  the user may pass either the first 9 character or the last six characters.  I need to match the remainder of the file name using either the first nine or last six.  I have read the help files for Regular Expression as used in R and I think what I want to use is glob2rx.  

I have worked a minimal example to test my code:

id <- "313929BL4FNMA2432.rds" 
cusip <- "313929BL4"
poolnumm <- "FNMA2432"
paste(cusip, ".*", ".rds")
glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE)

This returns false which leads me to believe that it is not working
glob2rx(paste(cusip, ".*", ".rds"), trim.head = TRUE, trim.tail = TRUE) == id

I am going to use as follows in the function below - which returns the error file not found

MBS_Test <- function(MBS.id = "character"){
MBS <- glob2rx(paste(MBS.id, ".*", "//.rds", sep = ""), trim.tail = TRUE)
MBS.Conn <- gzfile(description = paste(system.file(package = "BondLab"),
"/BondData/", MBS, sep = ""), open = "rb") 
MBS <- readRDS(MBS.Conn)
on.exit(close.connection(MBS.Conn))
return(MBS)
}   

Any help to get me in the right direction is appreciated - Glenn


More information about the R-help mailing list