1 May 2010 01:04
Re: drop last character in a names'vector
Marc Schwartz <marc_schwartz <at> me.com>
2010-04-30 23:04:01 GMT
2010-04-30 23:04:01 GMT
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)
(Continue reading)
RSS Feed