[R] drop last character in a names'vector

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


Dang it....you are right Greg.  

names <- c("ABC", "ABCD", "ABCDE", "ABCDEF")

> substr(names, 1, nchar(names) - 1)
[1] "AB"    "ABC"   "ABCD"  "ABCDE"


and...that is faster than using gsub().


Thanks for catching that....now it's time for cawfee....

Marc


On Apr 30, 2010, at 6:28 PM, Greg Snow wrote:

> No, substr is vectorized, you just have a typo, you are using a different vector for nchar than you are subsetting.
> 
> -- 
> 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 Marc Schwartz
>> Sent: Friday, April 30, 2010 5:04 PM
>> To: Sebastian Kruk
>> Cc: r-help at r-project.org; r-help at stat.math.ethz.ch
>> Subject: Re: [R] drop last character in a names'vector
>> 
>> 
>> 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