How to access the last value in a vector?


Question

Suppose I have a vector that is nested in a dataframe one or two levels. Is there a quick and dirty way to access the last value, without using the length() function? Something ala PERL's $# special var?

So I would like something like:

dat$vec1$vec2[$#]

instead of

dat$vec1$vec2[length(dat$vec1$vec2)]
1
256
4/29/2019 3:29:16 AM

I use the tail function:

tail(vector, n=1)

The nice thing with tail is that it works on dataframes too, unlike the x[length(x)] idiom.

331
7/31/2019 5:11:22 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon