[R] String processing - is there a better way

Hervé Pagès hpages at fhcrc.org
Thu Jul 22 23:59:04 CEST 2010

Hi Brian,

On 07/21/2010 10:02 AM, Davis, Brian wrote:
> Part 2)
> My next step in the string processing is to take the characters in the output of CleanRead and subtract 33 from the ascii value of the character to obtain an integer. Again I have a solution that works, involving splitting the string into characters then converting them to factors (starting at ascii 34) and using unclass to get the integer value. (kindof a atoi(x)-33 all in one step)
> I looked for the C equivalent of atoi, but the only help I could find (R-help 2003) suggested using as.numeric.  However, the help file (and testing) shows you get 'NA'.
> Am I missing an easier way to do this?

For this I found converting to raw with charToRaw(), then to integer
with as.integer(), then substracting, then converting back to character
to be pretty fast:

 > rawToChar(as.raw(
       as.integer(charToRaw("lajlkcjppstyyslkajalcmlkjla")) - 33))


> Thanks in advance,
> Brian
> ______________________________________________
> 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.

Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319

More information about the R-help mailing list