[R] splitting character strings and converting to numeric vectors

Jim Bouldin jrbouldin at ucdavis.edu
Thu May 6 14:12:40 CEST 2010


This seemingly should be quite simple but I can't solve it:

I have a long character vector of geographic data (data frame column named
"XY") whose elements vary in length (from 11 to 14 chars).  Each element is
structured as a set of digits, then an underscore, then more digits, e.g:

> data.frame(head(as.character(XY)))
  head.as.character.XY..
1         -448623_854854
2         -448563_854850
3         -448442_854842
4         -448301_854833
5         -448060_854818
6         -446828_854736

I simply need to separate the two sets of digits from each other and assign
them into new columns.  The closest I've been able to get is by:

> test=t(as.matrix(data.frame(head(strsplit(as.character(XY), "\\_")))))
> test
                       [,1]      [,2]    
c...448623....854854.. "-448623" "854854"
c...448563....854850.. "-448563" "854850"
c...448442....854842.. "-448442" "854842"
c...448301....854833.. "-448301" "854833"
c...448060....854818.. "-448060" "854818"
c...446828....854736.. "-446828" "854736"

So far so good, but  columns 1:2 will not coerce to either numeric or
integer, for unknown reasons.  Thanks for any help (and/or suggestions on a
better way to code this).



Jim Bouldin, PhD
Research Ecologist
Department of Plant Sciences, UC Davis
Davis CA, 95616
530-554-1740



More information about the R-help mailing list