[R] Using Sub
Rui Barradas
ruipbarradas at sapo.pt
Fri Jun 15 10:11:43 CEST 2012
Hello,
See inline.
Em 15-06-2012 08:03, Duncan Murdoch escreveu:
> On 12-06-14 6:23 PM, Lopez, Dan wrote:
>> Hi,
>>
>> I have been trying to get the sub function to work but can't for the
>> life of me figure it out. I tried looking at ?sub but I think there is
>> something that I am just not understanding.
>>
>> Example failed attempts
>>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off
>>> Step.accdb")
>> Error: '\C' is an unrecognized escape in character string starting "G:\C"
>
> This isn't anything to do with sub(), it's the third string that's the
> problem. When you say "G:\Comp..." it thinks you've escaped the C, but
> there's no such escape.
>
> Duncan Murdoch
>
This can be seen if we try to assign the second string.
s1 <- "G:\Compensation Audits_Reports\Step Audit\Steps off Step.accdb"
Error: '\C' is an unrecognized escape in character string starting "G:\C"
The right way would be
s2 <- 'G:\\Compensation Audits_Reports\\Step Audit\\Steps off Step.accdb'
pattern <- '\\\\'
gsub(pattern, '/', s2)
[1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb"
Note also the use of 'gsub', Global sub. I believe it's obvious that the
purpose is to substitute all backslashes by forward ones.
Hope this helps,
Rui Barradas
>>
>>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off
>>> Step.accdb",fixed=TRUE)
>> Error: '\C' is an unrecognized escape in character string starting "G:\C"
>>
>>> sub("\\","/","G:\Compensation Audits_Reports\Step Audit\Steps off
>>> Step.accdb",fixed=FALSE)
>> Error: '\C' is an unrecognized escape in character string starting
>> "G:\C")
>>
>>> sub("/","\","G:\Compensation Audits_Reports\Step Audit\Steps off
>>> Step.accdb")
>> Error: unexpected symbol in "sub("/","\","G"
>>
>> Thanks.
>> Dan
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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