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 ______________________________________________ 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.