[R] Using Sub

arun smartpink111 at yahoo.com
Mon Jun 18 18:37:38 CEST 2012


Hi Dan,

I am using linux.  So, not sure whether this will work or you tried it before.  After you paste the path at (1:), then press enter two times. One more point.  In your path, there were spaces within slashes.  So, if you do the scan, it will return as 5 items, which you have to join again.  I removed the spaces and tried this.


But you can try.

 str=scan(what="character",allowEscapes=F)
1: G:\CompensationAudits_Reports\StepAudit\StepsoffStep.accdb
2: 
Read 1 item
> str
[1] "G:\\CompensationAudits_Reports\\StepAudit\\StepsoffStep.accdb"
> gsub('\\\\', '/',str)
[1] "G:/CompensationAudits_Reports/StepAudit/StepsoffStep.accdb"


A.K.



----- Original Message -----
From: "Lopez, Dan" <lopez235 at llnl.gov>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, June 18, 2012 12:03 PM
Subject: RE: [R] Using Sub

Hi A.K.,

That works but when I copy and paste from Window explorer it will have a single backslashes and not double.  It works with double as what you have in your example below but not with single as what I would end up passing.

Do you have any other suggestions or methods to accomplish this?

Thanks.
Dan


-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com] 
Sent: Monday, June 18, 2012 8:59 AM
To: Lopez, Dan
Cc: R help
Subject: Re: [R] Using Sub

Hi,

I guess you need something like this:

> str1<-"G:\\Compensation Audits_Reports\\Step Audit\\Steps off Step.accdb"
> gsub('\\\\','/',str1)
[1] "G:/Compensation Audits_Reports/Step Audit/Steps off Step.accdb"


A.K.




----- Original Message -----
From: "Lopez, Dan" <lopez235 at llnl.gov>
To: Duncan Murdoch <murdoch.duncan at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, June 18, 2012 10:37 AM
Subject: Re: [R] Using Sub

Hi Duncan,

That's right but I want to be able to use this to replace back slashes with front slashes when I copy and paste file paths. How can I make this work so I don't get this error?

Thanks.
Dan

-----Original Message-----
From: Duncan Murdoch [mailto:murdoch.duncan at gmail.com] 
Sent: Friday, June 15, 2012 12:04 AM
To: Lopez, Dan
Cc: R help
Subject: Re: [R] Using Sub

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

>
>> 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