[R] drop last character in a names'vector

Marc Schwartz marc_schwartz at me.com
Sat May 1 01:04:01 CEST 2010


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



More information about the R-help mailing list