[R] Converting charter string to vector of numbers

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Wed Mar 25 20:34:21 CET 2015


No, you don't need the commas. All data in R are vectors, and as.numeric handles vectors just fine.

Keep in mind that what you see on the console is not necessarily what is in memory... the syntax you see or use to input data is just that... a tool for helping you communicate. For example, there are no actual quote marks around character strings in memory either, but they get printed to help us understand that they are character strings. Once you are referring to these values with variables they just work under the hood.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On March 25, 2015 10:27:40 AM PDT, Turgidson <epw at upenn.edu> wrote:
>Apologies for bringing up an old topic, but I am not finding working
>answers. 
>I am also an R newbie, so I'm still on the steep part of the learning
>curve.
>
>I have a character string :  "0.5 1 2 3 4"
>
>I need to convert it to a vector of numbers.  I get as far as
>strsplit(),
>which gives me 
>
> "0.5" "1" "2" "3" "4"
>
>this is still treated by R as a single item.  I need to get it to look
>like
>this:  "0.5", "1", "2", "3", "4"
>
>so that I can use as.numeric() and make it into a vector of numbers... 
> I
>have been going mad because for text operations, commas and quotes are
>loaded with meaning, and I keep going around in circles.
>
>Any help would be appreciated
>
>Thanks
>
>Paul
>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Converting-charter-string-to-vector-of-numbers-tp4705097.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list