[R] remove leading slash

Duncan Murdoch murdoch.duncan at gmail.com
Fri Jun 8 19:56:43 CEST 2012


On 08/06/2012 1:50 PM, Peter Langfelder wrote:
> On Fri, Jun 8, 2012 at 10:25 AM, David Winsemius<dwinsemius at comcast.net>  wrote:
> >
> >  On Jun 8, 2012, at 1:11 PM, Ben quant wrote:
> >
> >>  Hello,
> >>
> >>  How do I change this:
> >>>
> >>>  cnt_str
> >>
> >>  [1] "\002" "\001" "\102"
> >>
> >>  ...to this:
> >>>
> >>>  cnt_str
> >>
> >>  [1] "2" "1" "102"
> >>
> >>  Having trouble because of this:
> >>>
> >>>  nchar(cnt_str[1])
> >>
> >>  [1] 1
> >
> >
> >  "\001" is ASCII cntrl-A, a single character.
> >
> >  ?Quotes   # not the first, second or third place I looked but I knew I had
> >  seen it before.
>
> If you still want to obtain the actual codes, you will be able to get
> the number using utf8ToInt from package base or AsciiToInt from
> package sfsmisc. By default, the integer codes will be printed in base
> 10, though.

You could use

 > as.octmode(as.integer(charToRaw("\102")))
[1] "102"

if you really want the octal versions.  Doesn't work so well on "\1022" 
though (because that's two characters long).

Duncan Murdoch

>
> A roundabout way, assuming your are on a *nix system, would be to
> dump() cnt_str into a file, say tmp.txt, then run in a shell (or using
> system() ) something like
>
> sed --in-place 's/\\//g' tmp.txt
>
> to remove the slashes, then use
>
> cnt_str_new = read.table("tmp.txt")
>
> in R to get the codes back in. I'll let you iron out the details.
>
> Peter
>
> ______________________________________________
> 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