[R] Split string

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Apr 16 20:01:52 CEST 2009


Gabor Grothendieck wrote:
> Using string from another responder's post here are two
> solutions:
>

Er, a rather more obvious solution could be to point to the definition 
of substr...

> 1. The first converts to numeric and manipulates that:
[snip]
>> [37] "0.30" "  NA" "0.30" "0.30" "0.30" "0.30" "  NA" "  NA" "0.35"
>> "NA" "0.35" "0.30"
>> [49] "0.30" "0.40" "  NA" "0.40" "0.30" "  NA" "0.30" "0.30" "0.30"
>> "0.30" "0.45" "0.30"
>> [61] "0.30" "0.30" "0.30" "0.50" "0.30" "0.30" "0.45" "0.30"
>>
>> How do I output the number to the left of "." to variable X and the
>> two numbers to the right of "." to variable Y?
>>
>> FICB[,"x"] <- substr(FICB[,"temp2"],1,1)
>> Works, but
>>
>> FICB[,"y"] <- substr(FICB[,"temp2"],3,2)
>> only returns "". temp is class character.

That will happen when stop < start, try "...,3,4)"!

Alternatively sub("^[0-9]*\\.", "", ...) and sub("\\.[0-9]*$", "", ...)
should do the trick slightly more generally.

 > sub("^[0-9]*\\.", "", "0.30")
[1] "30"
 > sub("\\.[0-9]*$", "", "0.30")
[1] "0"

(and in either case, don't forget to get rid of the ".*NA.*" elements)

-- 
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907




More information about the R-help mailing list