Skip to content

Split string

5 messages · Peter Kraglund Jacobsen, Dimitris Rizopoulos, Gabor Grothendieck +1 more

#
[1] "0.30" "0.55" "0.45" "2.30" "0.45" "0.30" "0.25" "0.30" "0.30"
"1.05" "1.00" "1.00"
[13] "0.30" "0.30" "0.30" "0.55" "0.30" "0.30" "0.30" "0.25" "1.00"
"0.30" "0.30" "0.45"
[25] "0.30" "1.30" "0.30" "0.30" "0.45" "0.30" "0.30" "0.30" "  NA"
"NA" "  NA" "  NA"
[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.
#
try this:

string <- c("0.30", "0.55", "0.45", "2.30", "NA", "  NA", "  NA", 
"0.50", "0.30", "0.30", "0.45", "0.30")

splt <- strsplit(string, "\\.")
sapply(splt, function (x) if(length(x) == 2) x[1] else as.character(NA))
sapply(splt, function (x) if(length(x) == 2) x[2] else as.character(NA))


I hope it helps.

Best,
Dimitris
Peter Kraglund Jacobsen wrote:

  
    
#
Using string from another responder's post here are two
solutions:

1. The first converts to numeric and manipulates that:

cbind(part1 = floor(as.numeric(string)), part2 = 100 * as.numeric(string) %% 1)

2.  The second uses strapply from the gsubfn package.
It matches from the beginning ( ^ ) a string
of digits ( [0-9]+ ) followed by a dot ( [.] ) followed by a string of digits
to the end ( $ ) or ( | ) an NA possibly surrounded with spaces
(  *NA * ) and concatenates the result into a vector ( c ) and
simplifies all that by rbind'ing that together.   We still have character
data so in the second line we make it numeric:

library(gsubfn)
s <- strapply(string, "^([0-9]+)[.]([0-9]+)$|^ *NA *$", c, simplify = rbind)
apply(s, 2, as.numeric)


On Wed, Apr 15, 2009 at 4:02 AM, Peter Kraglund Jacobsen
<peter at kraglundjacobsen.dk> wrote:
1 day later
#
Gabor Grothendieck wrote:
Er, a rather more obvious solution could be to point to the definition 
of substr...
[snip]
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)
#
On Thu, Apr 16, 2009 at 2:01 PM, Peter Dalgaard
<p.dalgaard at biostat.ku.dk> wrote:
Only if there are a fixed number of digits.