Hi, I have a string like "1,2,5,7,8".>From this I want to create a numeric vector of length 5 (total numberof numbers in above string), with each element will be the number in above string. Is there any possibility to doing this? Thanks and regards,
Try this: x <- scan(textConnection("1,2,5,7,8"), sep = ",") On Wed, Sep 15, 2010 at 5:42 PM, Christofer Bogaso < bogaso.christofer@gmail.com> wrote:> Hi, I have a string like "1,2,5,7,8". > > From this I want to create a numeric vector of length 5 (total number > of numbers in above string), with each element will be the number in > above string. Is there any possibility to doing this? > > Thanks and regards, > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
> x <- "1,2,5,7,8" > as.numeric(unlist(strsplit(x, ',')))[1] 1 2 5 7 8 -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Christofer Bogaso > Sent: Wednesday, September 15, 2010 2:42 PM > To: r-help > Subject: [R] From string to numeric > > Hi, I have a string like "1,2,5,7,8". > > From this I want to create a numeric vector of length 5 (total number > of numbers in above string), with each element will be the number in > above string. Is there any possibility to doing this? > > Thanks and regards, > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi Cristofer, Try as.numeric(strsplit(x, ",")[[1]]) HTH, Jorge On Wed, Sep 15, 2010 at 4:42 PM, Christofer Bogaso <> wrote:> Hi, I have a string like "1,2,5,7,8". > > From this I want to create a numeric vector of length 5 (total number > of numbers in above string), with each element will be the number in > above string. Is there any possibility to doing this? > > Thanks and regards, > > ______________________________________________ > R-help@r-project.org mailing list > 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. >[[alternative HTML version deleted]]