Search code examples
rstring-substitutiongsubfn

gsubfn | Replace text using variables in Substitution


I am trying to remove a block of text that wraps around what I want to keep. So I wanted to assign variables since the text can be long. This is an example of what I am trying to do. [Doesn't remove the text]

Text<-'This is an example text [] test' 
topheader<-'This'
bottomheader<-'test'


gsubfn(".", list(topheader = "", bottomheader = ""), Text)
[1] "This is an example text [] test"


Goal: "is an example text []" 

Solution

  • 1) gsubfn There are several problems here:

    • the regular expression in gsubfn (and in gsub) must match the string you want to process but a dot matches only a single character so it can never match This or test which are 4 character strings. Use "\\w+" instead.

    • In list(a = x) the a must be a constant, not a variable. Write out the names explicitly or use setNames instead if they are in variables.

    Thus to fix up the code in the question:

    library(gsubfn)
    
    trimws(gsubfn("\\w+", list(This = "", text = ""), Text))
    ## [1] "is an example  [] test"
    

    or in terms of the header variables:

    L <- setNames(list("", ""), c(topheader, bottomheader))
    trimws(gsubfn("\\w+", L, Text))
    ## [1] "is an example  [] test"
    

    Note that this will replace any occurrence of topheader and bottomheader and not just ones at the start and end; however, this seems to be the closest to your code that is likely sufficient.

    2) sub Another possibility is this simple sub

    sub("^This (.*) text$", "\\1", Text)
    [1] "is an example  [] test"
    

    or in terms of the header variables:

    pat <- sprintf("^%s (.*) %s$", topheader, bottomheader)
    sub(pat, "\\1", Text)
    ## [1] "is an example  [] test"
    

    Update: Fixed (1)