[R] parsing numbers from a string

Liaw, Andy andy_liaw at merck.com
Mon Feb 9 01:31:30 CET 2004


Here's one possible way:

> xrange <- scan("clipboard", what="")
Read 7 items
> xrange
[1] "(0,74.4]"   "(74.4,149]" "(149,223]"  "(223,298]"  "(298,372]"
"(372,447]" 
[7] "(670,744]" 
> sapply(strsplit(substring(xrange, 2, nchar(xrange)-1), ","), as.numeric)
     [,1]  [,2] [,3] [,4] [,5] [,6] [,7]
[1,]  0.0  74.4  149  223  298  372  670
[2,] 74.4 149.0  223  298  372  447  744

HTH,
Andy


> From: Jonne Zutt
> 
> 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.
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list