DataMan
DataMan

Reputation: 169

How to get the second to last element in a Vector in R

I am trying to get the second to last element of a Vector into another vector. For an example see this data set

Vector A

7.31 15.54  9.79  4.24  9.01  6.26 12.05  5.16 11.13  7.69  6.42 13.65 11.17 11.04  6.80  7.15 11.52 10.14 10.07 8.30

I want to Create a

Vector B

  15.54  9.79  4.24  9.01  6.26 12.05  5.16 11.13  7.69  6.42 13.65 11.17 11.04  6.80  7.15 11.52 10.14 10.07 8.30

However I don't want to use

B<-A[2:20]

because with the time last element number will change(increases)

A[1:tail(A,n=1)]
[1]  7.31 15.54  9.79  4.24  9.01  6.26 12.05  5.16

This gives half of the data. Just 8 elements. Also tried

 B<-A[1:last(A)]
 print(B)
[1]  7.31 15.54  9.79  4.24  9.01  6.26 12.05  5.16

it gives only first few elements. why?

the answer I expect is

15.54  9.79  4.24  9.01  6.26 12.05  5.16 11.13  7.69  6.42 13.65 11.17 11.04  6.80  7.15 11.52 10.14 10.07 8.30

Upvotes: 4

Views: 10643

Answers (4)

Jonathan Bar
Jonathan Bar

Reputation: 46

Regarding short lists or vectors:

Both tail(A,-1) and A[2:length(A)] work the same for lists/vectors with length≥2, but 0-length and 1-length lists/vectors yield dissimilar results, favoring tail(A,-1).

Q0 = list()
Q1 = list(10)
C0 = c()
C1 = c(10)

tail(Q0 , -1)
# list()

tail(Q1 , -1)
# list()

tail(C0 , -1)
# NULL

tail(C1 , -1)
# numeric(0)

Q0[2:length(Q0)]
# [[1]]
# NULL
# 
# [[2]]
# NULL

Q1[2:length(Q1)]
# [[1]]
# NULL
# 
# [[2]]
# [1] 10

C0[2:length(C0)]
# NULL

C1[2:length(C1)]
# NA 10

This is especially important if you try to iterate over all elements, starting the second one:

for (item in C1[2:length(C1)]) {next} # would execute *twice*.
for (item in tail(C1, -1)) {next} # would *not* execute.

Upvotes: 0

Geoffrey Liddell
Geoffrey Liddell

Reputation: 41

The title is quite ambiguous, I thought it was going to be the second to last element in the sense of the second one from the end!

I'm curious to know why nobody answered with the seemingly obvious:

B <- A[-1]

maybe negative indexing uses tail() in any case? Hope I didn't completely misunderstand the question!!

Upvotes: 3

Timsy Suri
Timsy Suri

Reputation: 468

It seems you want to leave one element from the vector A. You can simply write B=tail(A,-1) where -1 leaves the first element.

Upvotes: 1

jay.sf
jay.sf

Reputation: 73782

Consider vector v containing ten elements.

v <- c(7.31, 15.54, 9.79, 4.24, 9.01, 6.26, 12.05, 5.16, 11.13, 7.69)

To get the 2nd to the last (i.e. 10th) element, you would write hard-coded,

v[2:10]
# [1] 15.54  9.79  4.24  9.01  6.26 12.05  5.16 11.13  7.69

which means inside the [ brackets you define the number of the element.

On the other hand,

v[2:tail(v, 1)]
# [1] 15.54  9.79  4.24  9.01  6.26 12.05

gives you different result, because tail() gives you the value of an element, which is in our example:

tail(v, 1)
# [1] 7.69

So actually you're saying,

v[2:7.69]
# [1] 15.54  9.79  4.24  9.01  6.26 12.05 

which is v[2:8] because 7.69 will be coerced as.integer(7.69) which yields 8.

You want to dynamically get the number of the last element, a task for length(), so what you want to do is:

v[2:length(v)]
# [1] 15.54  9.79  4.24  9.01  6.26 12.05  5.16 11.13  7.69

Upvotes: 2

Related Questions