[R] string-to-number
Marc Schwartz
MSchwartz at mn.rr.com
Sat Aug 19 14:58:46 CEST 2006
On Sat, 2006-08-19 at 13:30 +0100, Prof Brian Ripley wrote:
> On Sat, 19 Aug 2006, Marc Schwartz wrote:
>
> > On Sat, 2006-08-19 at 07:58 -0400, Charles Annis, P.E. wrote:
> > > Greetings, Amigos:
> > >
> > > I have been trying without success to convert a character string,
> > > > repeated.measures.columns
> > > [1] "3,6,10"
> > >
> > > into c(3,6,10) for subsequent use.
> > >
> > > as.numeric(repeated.measures.columns) doesn't work (likely because of the
> > > commas)
> > > [1] NA
> > > Warning message:
> > > NAs introduced by coercion
> > >
> > > I've tried many things including
> > > strsplit(repeated.measures.columns, split = ",")
> > >
> > > which produces a list with only one element, viz:
> > > [[1]]
> > > [1] "3" "6" "10"
> > >
> > > as.numeric() doesn't like that either.
> > >
> > > Clearly: 1) I cannot be the first person to attempt this, and 2) I've made
> > > this WAY harder than it is.
> > >
> > > Would some kind soul please instruct me (and perhaps subsequent searchers)
> > > how to convert the elements of a string into numbers?
> > >
> > > Thank you.
> >
> > One more step:
> >
> > > as.numeric(unlist(strsplit(repeated.measures.columns, ",")))
> > [1] 3 6 10
> >
> > Use unlist() to take the output of strsplit() and convert it to a
> > vector, before coercing to numeric.
>
> Or, more simply, use [[1]] as in
>
> as.numeric(strsplit(repeated.measures.columns, ",")[[1]])
>
> Also,
>
> eval(parse(text=paste("c(", repeated.measures.columns, ")")))
>
> looks competitive, and is quite a bit more general (e.g. allows spaces,
> works with complex numbers), or you can use scan() from an anonymous file
> or a textConnection.
I would say more than competitive:
repeated.measures.columns <- paste(1:100000, collapse = ",")
> str(repeated.measures.columns)
chr
"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,4"| __truncated__
> system.time(res1 <-
as.numeric(unlist(strsplit(repeated.measures.columns, ","))))
[1] 24.238 0.192 26.200 0.000 0.000
> system.time(res2 <- as.numeric(strsplit(repeated.measures.columns,
",")[[1]]))
[1] 24.313 0.196 26.471 0.000 0.000
> system.time(res3 <- eval(parse(text=paste("c(",
repeated.measures.columns, ")"))))
[1] 0.328 0.004 0.395 0.000 0.000
> str(res1)
num [1:100000] 1 2 3 4 5 6 7 8 9 10 ...
> str(res2)
num [1:100000] 1 2 3 4 5 6 7 8 9 10 ...
> str(res3)
num [1:100000] 1 2 3 4 5 6 7 8 9 10 ...
> all(res1 == res2)
[1] TRUE
> all(res1 == res3)
[1] TRUE
Best regards,
Marc
More information about the R-help
mailing list