Hi, i have a vector filled with names: [1] Alvaro Adela ... [25] Beatriz Berta ... ... [100000] ... I would like to drop last character in every name. I use the next program: for (i in 1:100000) { ? ? ? ? ? ? ? ? ? ? ? ? ? largo <- nchar(names[i]-1) ? ? ? ? ? ? ? ? ? ? ? ? ? names[i] <- substring (names[i],1,largo] ? ? ? ? ? ? ? ? ? ? ? ? ?} Is another and faster way of do it? Thanks, Sebasti?n.
The nchar and substring functions are both vectorized, you can do something like:> substring(state.name, 1, nchar(state.name)-1)And it should be much faster. -- 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 Sebastian Kruk > Sent: Friday, April 30, 2010 4:45 PM > To: r-help at r-project.org; r-help at stat.math.ethz.ch > Subject: [R] drop last character in a names'vector > > Hi, i have a vector filled with names: > > [1] Alvaro Adela ... > [25] Beatriz Berta ... > ... > [100000] ... > > I would like to drop last character in every name. > > I use the next program: > > for (i in 1:100000) { > ? ? ? ? ? ? ? ? ? ? ? ? ? largo <- nchar(names[i]-1) > ? ? ? ? ? ? ? ? ? ? ? ? ? names[i] <- substring (names[i],1,largo] > ? ? ? ? ? ? ? ? ? ? ? ? ?} > > Is another and faster way of do it? > > Thanks, > > Sebasti?n. > > ______________________________________________ > 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 Apr 30, 2010, at 5:44 PM, Sebastian Kruk wrote:> Hi, i have a vector filled with names: > > [1] Alvaro Adela ... > [25] Beatriz Berta ... > ... > [100000] ... > > I would like to drop last character in every name. > > I use the next program: > > for (i in 1:100000) { > largo <- nchar(names[i]-1) > names[i] <- substring (names[i],1,largo] > } > > Is another and faster way of do it? > > Thanks, > > Sebasti?n.As is the case with R, more than one, but the fastest may be: names <- c("Alvaro Adela", "Beatriz Berta")> gsub("^(.*).{1}$", "\\1", names)[1] "Alvaro Adel" "Beatriz Bert" Just to show that it works with entries of varying lengths:> gsub("^(.*).{1}$", "\\1", c("ABC", "ABCD", "ABCDE", "ABCDEF"))[1] "AB" "ABC" "ABCD" "ABCDE" See ?gsub and ?regex You could use substr(), but the arguments for substring lengths are not vectorized, so the following won't work:> substr(c("ABC", "ABCD", "ABCDE", "ABCDEF"), 1, nchar(names) - 1)[1] "ABC" "ABCD" "ABCDE" "ABCDEF" You would have to do something like this:> as.vector(sapply(c("ABC", "ABCD", "ABCDE", "ABCDEF"),function(x) substr(x, 1, nchar(x) - 1))) [1] "AB" "ABC" "ABCD" "ABCDE" See ?substr and ?nchar HTH, Marc Schwartz