[R] parsing numbers from a string

Gabor Grothendieck ggrothendieck at myway.com
Mon Feb 9 07:10:16 CET 2004


Try this:

   sapply( parse( text = chartr( "(],", "  +", z ) ), eval ) / 2

chartr translates ( to space, ] to space and comma to +.  Then
the character strings are parsed and evaluated as R expressions.
Finally, we divide by 2.

---
Date:   Sun, 8 Feb 2004 21:39:13 +0100 
From:   Jonne Zutt <j.zutt at ewi.tudelft.nl>
To:   <r-help at stat.math.ethz.ch> 
Subject:   [R] parsing numbers from a string 

 
Dear R-help members,

I have several large data sets from certain simulations I did and now I want to plot the results nicely.
I don't know anything about the size of the x and y values in advance.
Plotting these values is not a problem.
However, I want to add errorbars (errbar in the Hmisc package).

1) For this I'm factoring the data (xdata00 varies from 0 to max(xdata00))
     xfactor00 = factor(cut(xdata00, breaks = (max(xdata00)/10)*(0:100)))

2) I compute the means of the different levels
     ymeans00 = tapply(ydata00, xfactor00, mean)

3) I compute the errors of the different levels
     ystdevs00 = tapply(ydata00, xfactor00, sd)

4) And then I use the errbar function
     errbar((xlim/20)+(xlim/10)*(0:(length(ymeans00)-1)), ymeans00, ymeans00+ystdevs00, ymeans00-ystdevs00, add=T)

My problem is that the x-values that I provide to the errbar are not
correct if there are empty parts in my initial data.

To give an example of this:
> ymeans00
(0,74.4] (74.4,149] (149,223] (223,298] (298,372] (372,447] (670,744]
20.74706 195.90000 275.62500 316.00000 329.75000 373.00000 478.75000

Here we see the 6th and 7th intervals are not neighbors, in fact there are three missing.
The errbar function now plots the last error bar at a too small x value.

A possible solution for me would be to compute the correct x value from the following output
> names(ymeans00)
[1] "(0,74.4]" "(74.4,149]" "(149,223]" "(223,298]" "(298,372]"
[6] "(372,447]" "(670,744]"

I want a vector containing [0+74.4/2, 74.4+149/2, ...etc]

But I don't know how to parse these strings.
Does anyone know how to do this, or maybe is there a simpler way?

Thanks in advance,
Jonne.




More information about the R-help mailing list