[R] String Matching

Duncan Murdoch murdoch.duncan at gmail.com
Sat Jan 30 03:02:27 CET 2016


On 29/01/2016 12:02 PM, Glenn Schultz wrote:
> 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

No, it returns a regular expression.  You need to tell us what you 
really did if you want help fixing it.

Duncan Murdoch

> 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
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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