Re: [R] How to convert "c:\a\b" to "c:/a/b"?

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Tue 28 Jun 2005 - 22:38:49 EST


On 27-Jun-05 Spencer Graves wrote:
> Thanks, Dirk, Gabor, Eric:
>
> You all provided appropriate solutions for the stated problem.
> Sadly, I oversimplified the problem I was trying to solve: I copy a
> character string giving a DOS path from MS Windows Explorer into an R
> script file, and I get something like the following:
>
> D:\spencerg\statmtds\R\Rnews
>
> I want to be able to use this in R with its non-R meaning,
e.g., in
> readLine, count.fields, read.table, etc., after appending a file name.
> Your three solutions all work for my oversimplified toy example but are
> inadequate for the problem I really want to solve.

The various responses show that solving this problem directly within R may be, well, problematic!

I'm not a perl user, but possibly Spencer Graves's speculation might eventually be brought into a straightforward solution.

Unfortunately, for this query, I'm not a Windows users either, so can't be authoritative about how practical the following may be for Spencer's problem (and similar).

In the Unix world, the "string editor" program 'sed' simply mops up problems of this kind. For example, I just did:

  echo "D:\spencerg\statmtds\R\Rnews" | sed 's@\\@/@g'

and got the response:

  D:/spencerg/statmtds/R/Rnews

Note: the parsing of the 'sed' command is as follows:

  s@x@y@g means substitute "y" for every ("g" = "global") occurrence

          of "x".

  The character to be replaced (x="\") is the escape character so   needs to be escaped ("\\"); but apart from this it's straightforward.

  The usual separator is "/" instead of "@", but I used "@"   to simplify things since the substitute itself is y="/".

  An alternative using "/" as separator would be

    echo "D:\spencerg\statmtds\R\Rnews" | sed 's/\\/\//g'     D:/spencerg/statmtds/R/Rnews

  which is a bit more complicated but still straightforward   (depending on your eyesight). Here, both x="\" and y="/"   need to be escaped.

'sed', and a lot of other useful stuff, is available as "GNU tools" which can be installed on Windows, and allow this kind of thing to be very slickly done, but outside of R of course.

I also make much use of 'awk' (but you can equally use perl) for tidying up CSV files exported from Excel, and for all sorts of rearrangements and substitutions in data files. While Unix users take this sort of thing for granted, I suggest to Windows users that it could be well worth installing the GNU tools, since they are very useful indeed.

It's not clear from Spencer's follow-up whether doing this kind of preliminary work outside of R, and then bringing the results into R (e.g. via the clipboard or a file) is practical in his context. If not (i.e. all the work has to be done inside R), then of course my sugestion above is not helpful in this case!

Best wishes,
Ted.



E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 28-Jun-05                                       Time: 13:38:37
------------------------------ XFMail ------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jun 28 22:50:55 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:04 EST