i have a vector with values similar to the below text [1] 001-010-001-0 I want to get rid of all leading zeroes. for example i want to change the values of the vector so that [1] 001-010-001-0 becomes [1] 1-010-001-0. Another example [1]082-232-232-1 becomes [1] 82-232-232-1 how do i do this? -- View this message in context: http://www.nabble.com/cutting-out-numbers-from-vectors-tp18763058p18763058.html Sent from the R help mailing list archive at Nabble.com.
Have a look at gsub:> x <- "001-010-001-0" > gsub("^0+", "", x)[1] "1-010-001-0" -Christos> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of calundergrad > Sent: Thursday, July 31, 2008 4:40 PM > To: r-help at r-project.org > Subject: [R] cutting out numbers from vectors > > > i have a vector with values similar to the below text [1] > 001-010-001-0 > > I want to get rid of all leading zeroes. for example i want > to change the values of the vector so that [1] 001-010-001-0 > becomes [1] 1-010-001-0. > > Another example > [1]082-232-232-1 becomes [1] 82-232-232-1 > > how do i do this? > -- > View this message in context: > http://www.nabble.com/cutting-out-numbers-from-vectors-tp18763 > 058p18763058.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. > >
This is something that is easier done in C than in R (to the best of my very limited knowledge). To do this in R you could do something like:> x <- "082-232-232-1" > y <-unlist(strsplit(x,"")) > i <- which(y != "0")[1]-1 > paste(y[-(1:i)],collapse="")[1] "82-232-232-1" --- On Fri, 1/8/08, calundergrad <haibinglin at berkeley.edu> wrote:> From: calundergrad <haibinglin at berkeley.edu> > Subject: [R] cutting out numbers from vectors > To: r-help at r-project.org > Received: Friday, 1 August, 2008, 6:40 AM > i have a vector with values similar to the below text > [1] 001-010-001-0 > > I want to get rid of all leading zeroes. for example i > want to change the > values of the vector so that [1] 001-010-001-0 becomes [1] > 1-010-001-0. > > Another example > [1]082-232-232-1 becomes [1] 82-232-232-1 > > how do i do this? > -- > View this message in context: > http://www.nabble.com/cutting-out-numbers-from-vectors-tp18763058p18763058.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
On Thu, 31 Jul 2008, calundergrad wrote:> i have a vector with values similar to the below text > [1] 001-010-001-0 > > I want to get rid of all leading zeroes. > for example i want to change the values of the vector > so that [1] 001-010-001-0 becomes [1] 1-010-001-0. > > Another example > [1]082-232-232-1 becomes [1] 82-232-232-1 >xform <- function(nstr) { nstr.vec <- unlist(strsplit(nstr, '-')) nstr.vec[1] <- as.character(as.integer(nstr.vec[1])) return(paste(nstr.vec, collapse='-')) } stopifnot(xform('001-010-001-0') == '1-010-001-0') stopifnot(xform('082-232-232-1') == '82-232-232-1') ---------------------------------------------------------- SIGSIG -- signature too long (core dumped)