R: How to replace space (' ') in string with a *single* backslash and space ('\ ')

前端 未结 2 886
刺人心
刺人心 2020-12-19 00:54

I\'ve searched many times, and haven\'t found the answer here or elsewhere. I want to replace each space \' \' in variables containing file names with a \

2条回答
  •  遥遥无期
    2020-12-19 01:15

    Very helpful discussion! (I've been Googling the heck out of this for 2 days.)

    Another way to see the difference (rather than writing to a file) is to compare the contents of the string using print and cat.

    z <- gsub(" ", "\\", "a b", fixed = TRUE)
    
    > print(z)
    [1] "a\\ b"
    
    > cat(z)
    a\ b
    

    So, by using cat instead of print we can confirm that the gsub line is doing what was intended when we're trying to add single backslashes to a string.

提交回复
热议问题