[R] substitution of the ASCII character "squared" AKA "^2" AKA (alt+0178) with a tractable one

David Winsemius dwinsemius at comcast.net
Tue Apr 10 15:54:54 CEST 2012


On Apr 10, 2012, at 9:44 AM, ottorino wrote:

> Deae R helpers,
> the problem I'm facing today is related to the manipulation of a  
> string.
>
> The string is coming from a a porosimeter, whose control is under a
> complicate set-up of two computers
>
> One (running on DOS) is controlling directly the hardware, while the
> other (running on win XP) which process the data is connected to the
> first one via serial. I get the data from this last computer.
>
> The string I'm trying to process comes from a command like this
>
>> myString <- readLines(path.to.file, 150)[78]
>
> and looks like this
>
>> [1] "(MPa)\t(mm3)\t(nM)\t(mm3/g)\t(mm3/g)\t(%)\t(m\xb2/g)\t"
>
> where "\xb2" is the alt+0178 ASCII character
>

Perhaps, modulo encoding issues I'm not expert in, one more backslash  
than you tried:

 > gsub( "\\\xb2" , "2", "(MPa)\t(mm3)\t(nM)\t(mm3/g)\t(mm3/g)\t(%)\t(m 
\xb2/g)\t")
[1] "(MPa)\t(mm3)\t(nM)\t(mm3/g)\t(mm3/g)\t(%)\t(m2/g)\t"


> My problem is to find out the regexp to substitute the ASCII 0178  
> with a
> simple "2"
>
> No problem with, say
>
> gsub('\t',';', myString)
>
> [1] "(MPa);(mm3);(nM);(mm3/g);(mm3/g);(%);(m\U3e32623c/g);"
>
> which incidentally change the "\xb2" to "\U3e32623c",
>
> but I cannot find a way to search and substitute for "\xb2".
>
> I've tried many combinations such as
> gsub('\xb', ...
> gsub('\\xb', ...
> gsub('\\xb2', ...
> gsub('\xb2', ... etc
>
> without success.
>
> This is surely due to my ignorance of regexp, but I suspect something
> arduous here (at least for me !!).
>
> Further than this, consider that I'm running a Linux box and that I  
> have
> to transfer the code to a mac.......
>
> I've tried also to see if it possible to change that string at the
> source, but it is possible only on manual basis, quite inconvenient if
> you have to process many files.
>
> Any help ? I'm quite discouraged ...
>
>
> -- 
> Ottorino-Luca Pantani, Università di Firenze
> Dip.to di Scienze delle Produzioni Vegetali,
> del Suolo e dell'Ambiente Forestale (DiPSA)
> P.zle Cascine 28 50144 Firenze Italia
> Ubuntu 10.04 -- GNOME 2.30.2
> GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version
> 2.18.0)
> ESS version 12.04 -- R  2.15.0
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list