Getting the last n elements of a vector. Is there a better way than using the length() function?
see ?tail
and ?head
for some convenient functions:
> x <- 1:10
> tail(x,5)
[1] 6 7 8 9 10
For the argument's sake : everything but the last five elements would be :
> head(x,n=-5)
[1] 1 2 3 4 5
As @Martin Morgan says in the comments, there are two other possibilities which are faster than the tail solution, in case you have to carry this out a million times on a vector of 100 million values. For readibility, I'd go with tail.
test elapsed relative
tail(x, 5) 38.70 5.724852
x[length(x) - (4:0)] 6.76 1.000000
x[seq.int(to = length(x), length.out = 5)] 7.53 1.113905
benchmarking code :
require(rbenchmark)
x <- 1:1e8
do.call(
benchmark,
c(list(
expression(tail(x,5)),
expression(x[seq.int(to=length(x), length.out=5)]),
expression(x[length(x)-(4:0)])
), replications=1e6)
)
R shortcut to getting last n entries in a vector
You want the tail
function
foo <- 1:23
tail(foo, 5)
#[1] 19 20 21 22 23
tail(foo, 7)
#[1] 17 18 19 20 21 22 23
x <- 1:3
# If you ask for more than is currently in the vector it just
# returns the vector itself.
tail(x, 5)
#[1] 1 2 3
Along with head
there are easy ways to grab everything except the last/first n elements of a vector as well.
x <- 1:10
# Grab everything except the first element
tail(x, -1)
#[1] 2 3 4 5 6 7 8 9 10
# Grab everything except the last element
head(x, -1)
#[1] 1 2 3 4 5 6 7 8 9
Getting the length of a vector without using the length() function
As suggested in the comments, here are several ways :
max(seq_along(v1))
tail(seq_along(v1), 1)
nrow(as.data.frame(vector))
Though I really don't know why you want to do that. Length is a Primitive — so calls C code —, and you will hardly find a faster solution (if this is what you're looking for).
> length
function (x) .Primitive("length")
Best
Colin
R vector staying the same length after indexing within recursive function
The cause of the strange behavior of the code is an error in indexing of the vector elements. The part mid+1:length(vec)
should be (mid+1):length(vec)
because the :
operator is executed before addition.
Here is an illustration of the difference.
5 + 1:10
# [1] 6 7 8 9 10 11 12 13 14 15
(5+1):10
# [1] 6 7 8 9 10
Using name of vector argument in function
I added deparse(substitute(vector))
to get the name of the object as a string.
func <- function(vector, minimumvalue){
if(length(vector)==1){
return(1)}
else if (length(vector)<minimumvalue){
stop(paste0("The length of ",deparse(substitute(vector))," is less than ",minimumvalue))}
else if (length(vector)>minimumvalue){
stop(paste0("The length of ",deparse(substitute(vector))," is more than ",minimumvalue))}
else (return(vector))
}
colours <- c("red","green","blue")
func(colours, 2)
Error in func(colours, 2) : The length of colours is more than 2
Subsetting a vector with variables changes the length of the result vector
it is a probelm of parenthesis
x <- 1:10
data_starts_at <- 2
data_starts_at+1:10
[1] 3 4 5 6 7 8 9 10 11 12
# it means that you add data_starts_at at the vector 1:10
# then the final NA is because you ask for data that does not exist (11 & 12)
x[(data_starts_at+1):10]
[1] 3 4 5 6 7 8 9 10 #it's ok
hth
Related Topics
Same Function Over Multiple Data Frames in R
How to Insert an Image into the Navbar on a Shiny Navbarpage()
Dplyr on Data.Table, am I Really Using Data.Table
How to Extract the Row with Min or Max Values
Change Row Order in a Matrix/Dataframe
Is There a Way of Manipulating Ggplot Scale Breaks and Labels
Solution. How to Install_Github When There Is a Proxy
Dplyr: Lead() and Lag() Wrong When Used with Group_By()
Predict.Lm() with an Unknown Factor Level in Test Data
Reading 40 Gb CSV File into R Using Bigmemory
How to Randomize (Or Permute) a Dataframe Rowwise and Columnwise
How to Get Coefficients and Their Confidence Intervals in Mixed Effects Models
How Convert Decimal to Posix Time