[R] Converting charter string to vector of numbers (Turgidson)

Manel Amado Martí amado at cambrasabadell.org
Thu Mar 26 09:52:39 CET 2015


Hi,

See that strsplit() retorns a list object. Then, to transform a character vector (that is an element into the list), to a numèric vector, you should access directly to that element to aply as.numeric. The code is:

aux<-"0.5 1 2 3 4"
vectnum<-strsplit(aux," ")
class(vectnum)	## see that is returning a list
numaux<-as.numeric(vectnum[[1]])	## get the nummerical vector form the vector object that stays into the vectnum list


Message: 20
Date: Wed, 25 Mar 2015 10:27:40 -0700 (PDT)
From: Turgidson <epw at upenn.edu>
To: r-help at r-project.org
Subject: [R] Converting charter string to vector of numbers
Message-ID: <1427304460123-4705097.post at n4.nabble.com>
Content-Type: text/plain; charset=us-ascii

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.


    Manel Amado i Martí
Cap d'Assessoria de Comerç Interior
amado at cambrasabadell.org
Tel. 93 745 12 63 . Fax 93 745 12 64      
Av. Francesc Macià, 35 . 08206 Sabadell
Apt. corr. 119 . www.cambrasabadell.org  



More information about the R-help mailing list