Hi Everyone. I have a simple problem but don't know, how to get along. how can I convert the vector a<-c("0,01","1,00") in a vector b<-c(0.01,1.00) Thank you for suggestions M.Kirschbaum [[alternative HTML version deleted]]
On Friday 12 September 2003 14:29, Michael Kirschbaum wrote:> Hi Everyone. > > I have a simple problem but don't know, how to get along. > > how can I convert the vector > > a<-c("0,01","1,00") > in a vector > b<-c(0.01,1.00)You can use as.numeric(), but you need to change the decimal delimiter to ".", e.g. R> as.numeric(gsub(",", ".", a)) [1] 0.01 1.00 hth, Z> Thank you for suggestions > > M.Kirschbaum > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
"Michael Kirschbaum" <emkiba at gmx.de> writes:> Hi Everyone. > > I have a simple problem but don't know, how to get along. > > how can I convert the vector > > a<-c("0,01","1,00") > in a vector > b<-c(0.01,1.00) > > Thank you for suggestions[Expect about 10 people to chime in...] as.numeric(sub(",", ".", a)) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Michael Kirschbaum schrieb:> Hi Everyone. > > I have a simple problem but don't know, how to get along. > > how can I convert the vector > > a<-c("0,01","1,00") > in a vector > b<-c(0.01,1.00) > > Thank you for suggestionsAh, it seems to be a common German problem. My solution is: as.numeric(sub(",",".",a)) Thomas
as.numeric(gsub(",", ".", a)) works if the locale is such that decimal points are used instead of commas, but you might seek a more generic solution and have R use a comma for decimal separation - see ?localeconv.> -----Original Message----- > From: Michael Kirschbaum [mailto:emkiba at gmx.de] > Sent: 12 September 2003 13:29 > To: R-Help > Subject: [R] convert a Character-string to a number > > > Security Warning: > If you are not sure an attachment is safe to open please contact > Andy on x234. There are 0 attachments with this message. > ________________________________________________________________ > > Hi Everyone. > > I have a simple problem but don't know, how to get along. > > how can I convert the vector > > a<-c("0,01","1,00") > in a vector > b<-c(0.01,1.00) > > Thank you for suggestions > > M.Kirschbaum > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}
?as.numeric On Fri, 12 Sep 2003, Michael Kirschbaum wrote:> Hi Everyone. > > I have a simple problem but don't know, how to get along. > > how can I convert the vector > > a<-c("0,01","1,00") > in a vector > b<-c(0.01,1.00) > > Thank you for suggestions > > M.Kirschbaum > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-- 620B Bartram Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704
Michael Kirschbaum wrote:> how can I convert the vector > > a<-c("0,01","1,00") > in a vector > b<-c(0.01,1.00)What is your decimal delimiter ? '.' or ',' ? If it is ',' you should change it into '.' and then as.numeric() works just fine. If it is '.' it won't work. HTH, Laurent
Try as.numeric(gsub(",",".",a)) Hope it helps!! Best regards Kenneth On Fri, 12 Sep 2003 14:29:13 +0200, Michael Kirschbaum <emkiba at gmx.de> wrote:> Hi Everyone. > > I have a simple problem but don't know, how to get along. > > how can I convert the vector > > a<-c("0,01","1,00") > in a vector b<-c(0.01,1.00) > > Thank you for suggestions > > M.Kirschbaum > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >--