This is unlikely to be the kind of operation where speed is essential, 
but nevertheless on my build of 2.14.0 (with byte compiled base packages):

stopifnot(getRversion()>= "2.14")
library("compiler")

f1<- function (x, n) head(x, length(x) - n)# suggested by baptiste auguie
f2<-  function (x, n) x[ seq(1, length(x) - n) ]    # suggested by baptiste 
auguie
f3<- function (x, n) x[ - seq(length(x), by=-1, length=n) ]   # suggested by 
baptiste auguie
f4<- function (x, n) head(x, -n)# Suggested by Petr PIKAL
f5<- function (x, n) x[ seq_len(length(x) - n) ]# My variation on f2
c1<- cmpfun(f1,options = list(optimize = 3))# Undocumented (?) default is 2
c2<- cmpfun(f2,options = list(optimize = 3))
c3<- cmpfun(f3,options = list(optimize = 3))
c4<- cmpfun(f4,options = list(optimize = 3))
c5<- cmpfun(f5,options = list(optimize = 3))
library("rbenchmark")
set.seed(42)
x<- runif(1e6)

n<- 1
benchmark(f1(x, n), f2(x, n), f3(x, n), f4(x, n), f5(x, n),
           c1(x, n), c2(x, n), c3(x, n), c4(x, n), c5(x, n),
           columns = c("test", "elapsed", "relative"),
           order = "relative", replications = 1e3)
#        test elapsed relative
# 7  c2(x, n)  17.483 1.000000
# 10 c5(x, n)  17.600 1.006692
# 1  f1(x, n)  22.782 1.303094
# 2  f2(x, n)  23.054 1.318652
# 4  f4(x, n)  23.498 1.344049
# 9  c4(x, n)  23.547 1.346851
# 6  c1(x, n)  23.752 1.358577
# 5  f5(x, n)  23.892 1.366585
# 3  f3(x, n)  27.209 1.556312
# 8  c3(x, n)  27.296 1.561288
## So c{2,5} are fastest, .3 slowest, and the rest much the same

n<- 1e5
benchmark(f1(x, n), f2(x, n), f3(x, n), f4(x, n), f5(x, n),
           c1(x, n), c2(x, n), c3(x, n), c4(x, n), c5(x, n),
           columns = c("test", "elapsed", "relative"),
           order = "relative", replications = 1e3)
#        test elapsed relative
# 10 c5(x, n)  14.729 1.000000
# 7  c2(x, n)  14.939 1.014258
# 5  f5(x, n)  18.544 1.259013
# 2  f2(x, n)  18.557 1.259895
# 6  c1(x, n)  18.695 1.269265
# 4  f4(x, n)  18.718 1.270826
# 9  c4(x, n)  18.729 1.271573
# 1  f1(x, n)  18.729 1.271573
# 3  f3(x, n)  20.836 1.414624
# 8  c3(x, n)  20.958 1.422907
## As before

n<- 1
enableJIT(2)# Will also optimise rbenchmark::benchmark.
benchmark(f1(x, n), f2(x, n), f3(x, n), f4(x, n), f5(x, n),
           c1(x, n), c2(x, n), c3(x, n), c4(x, n), c5(x, n),
           columns = c("test", "elapsed", "relative"),
           order = "relative", replications = 1e3)
#        test elapsed relative
# 7  c2(x, n)  15.035 1.000000
# 10 c5(x, n)  15.076 1.002727
# 2  f2(x, n)  15.258 1.014832
# 9  c4(x, n)  15.313 1.018490
# 5  f5(x, n)  15.327 1.019421
# 6  c1(x, n)  15.402 1.024410
# 4  f4(x, n)  15.430 1.026272
# 1  f1(x, n)  15.594 1.037180
# 3  f3(x, n)  18.238 1.213036
# 8  c3(x, n)  18.245 1.213502
## No difference between the c. and f. functions here


So as long as you don't use the .3 functions you should be OK.

Allan
http://www.cybaea.net/Blogs/Data/

On 18/04/11 07:07, Petr PIKAL wrote:
> Hi
>
> r-help-boun...@r-project.org napsal dne 18.04.2011 04:51:20:
>
>> Or perhaps even more parsimoniously (by a couple of characters) -
>>
>> r<- c(1, 2, 3, 4, 5)
>> r2<-r[-length(r)]
> Maybe even shorter
>
> head(x,-1)
>
> Regards
> Petr
>
>
>> Min-Han
>>
>> On Sun, Apr 17, 2011 at 10:23 PM, Daisy Englert Duursma<
>> daisy.duur...@gmail.com>  wrote:
>>
>>> A easier solution:
>>>
>>> r<- c(1, 2, 3, 4, 5)
>>> r2<-r[1:length(r)-1]
>>>
>>>
>>>
>>>
>>> On Mon, Apr 18, 2011 at 10:51 AM, empyrean<ctr...@ucdavis.edu>  wrote:
>>>> Hey guys,
>>>>
>>>> I've search a few threads about deleting a value from a vector, but
> no
>>> one
>>>> has addressed this question so far.
>>>>
>>>> I want to delete the last value from a string of values
>>>>
>>>> I have:
>>>>
>>>> r = [ 1, 2, 3, 4, 5 ], and i want to make r2 = to [ 1, 2, 3, 4]
>>>>
>>>> So that r2 is just like r, except that it missing the final value.
>>>>
>>>> Thanks,
>>>>
>>>> --
>>>> View this message in context:
>>> http://r.789695.n4.nabble.com/Deleting-the-last-value-of-a-vector-
>> tp3456363p3456363.html
>>>> Sent from the R help mailing list archive at Nabble.com.
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>>>
>>> --
>>> Daisy Englert Duursma
>>> Department of Biological Sciences
>>> Room E8C156
>>> Macquarie University, North Ryde, NSW 210
>>> Australia
>>>
>>> Tel +61 2 9850 9256
>>>
>>> ______________________________________________
>>> 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.
>>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
> ______________________________________________
> 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.

        [[alternative HTML version deleted]]

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

Reply via email to