<p>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 <code>length()</code> function?  Something ala PERL's <code>$#</code> special var?</p>

<p>So I would like something like:</p>

<pre><code>dat$vec1$vec2[$#]
</code></pre>

<p>instead of</p>

<pre><code>dat$vec1$vec2[length(dat$vec1$vec2)]
</code></pre>

[{'answer_id': 83162, 'author': 'Gregg Lind', 'author_id': 15842, 'author_profile': 'https://Stackoverflow.com/users/15842', 'pm_score': 7, 'selected': False, 'text': "<p>If you're looking for something as nice as Python's x[-1] notation, I think you're out of luck.  The standard idiom is</p>\n\n<pre><code>x[length(x)]  \n</code></pre>\n\n<p>but it's easy enough to write a function to do this:</p>\n\n<pre><code>last &lt;- function(x) { return( x[length(x)] ) }\n</code></pre>\n\n<p>This missing feature in R annoys me too!</p>\n"}, {'answer_id': 83222, 'author': 'lindelof', 'author_id': 1428, 'author_profile': 'https://Stackoverflow.com/users/1428', 'pm_score': 9, 'selected': False, 'text': '<p>I use the <code>tail</code> function:</p>\n\n<pre><code>tail(vector, n=1)\n</code></pre>\n\n<p>The nice thing with <code>tail</code> is that it works on dataframes too, unlike the <code>x[length(x)]</code> idiom.</p>\n'}, {'answer_id': 153852, 'author': 'Florian Jenn', 'author_id': 23813, 'author_profile': 'https://Stackoverflow.com/users/23813', 'pm_score': 6, 'selected': False, 'text': '<p>Combining <a href="https://stackoverflow.com/a/83222/10221765">lindelof\'s</a> and <a href="https://stackoverflow.com/a/83162/10221765">Gregg Lind\'s</a> ideas:</p>\n\n<pre><code>last &lt;- function(x) { tail(x, n = 1) }\n</code></pre>\n\n<p>Working at the prompt, I usually omit the <code>n=</code>, i.e. <code>tail(x, 1)</code>.</p>\n\n<p>Unlike <code>last</code> from  the <code>pastecs</code> package, <code>head</code> and <code>tail</code> (from <code>utils</code>) work not only on vectors but also on data frames etc., and also can return data "<em>without first/last n elements</em>", e.g. </p>\n\n<pre><code>but.last &lt;- function(x) { head(x, n = -1) }\n</code></pre>\n\n<p>(Note that you have to use <code>head</code> for this, instead of <code>tail</code>.)</p>\n'}, {'answer_id': 21706190, 'author': 'James', 'author_id': 269476, 'author_profile': 'https://Stackoverflow.com/users/269476', 'pm_score': 4, 'selected': False, 'text': '<p>Another way is to take the first element of the reversed vector:</p>\n\n<pre><code>rev(dat$vect1$vec2)[1]\n</code></pre>\n'}, {'answer_id': 23638765, 'author': 'scuerda', 'author_id': 1748028, 'author_profile': 'https://Stackoverflow.com/users/1748028', 'pm_score': 4, 'selected': False, 'text': '<p>I just benchmarked these two approaches on data frame with 663,552 rows using the following code:</p>\n\n<pre><code>system.time(\n  resultsByLevel$subject &lt;- sapply(resultsByLevel$variable, function(x) {\n    s &lt;- strsplit(x, ".", fixed=TRUE)[[1]]\n    s[length(s)]\n  })\n  )\n\n user  system elapsed \n  3.722   0.000   3.594 \n</code></pre>\n\n<p>and</p>\n\n<pre><code>system.time(\n  resultsByLevel$subject &lt;- sapply(resultsByLevel$variable, function(x) {\n    s &lt;- strsplit(x, ".", fixed=TRUE)[[1]]\n    tail(s, n=1)\n  })\n  )\n\n   user  system elapsed \n 28.174   0.000  27.662 \n</code></pre>\n\n<p>So, assuming you\'re working with vectors, accessing the length position is significantly faster.</p>\n'}, {'answer_id': 27992356, 'author': 'Akash ', 'author_id': 2682018, 'author_profile': 'https://Stackoverflow.com/users/2682018', 'pm_score': 4, 'selected': False, 'text': '<p>I have another method for finding the last element in a vector.\nSay the vector is <code>a</code>.</p>\n\n<pre><code>&gt; a&lt;-c(1:100,555)\n&gt; end(a)      #Gives indices of last and first positions\n[1] 101   1\n&gt; a[end(a)[1]]   #Gives last element in a vector\n[1] 555\n</code></pre>\n\n<p>There you go!</p>\n'}, {'answer_id': 32510333, 'author': 'Kurt Ludikovsky', 'author_id': 4934536, 'author_profile': 'https://Stackoverflow.com/users/4934536', 'pm_score': 3, 'selected': False, 'text': '<p>Whats about</p>\n\n<pre><code>&gt; a &lt;- c(1:100,555)\n&gt; a[NROW(a)]\n[1] 555\n</code></pre>\n'}, {'answer_id': 37238415, 'author': 'anonymous', 'author_id': 179927, 'author_profile': 'https://Stackoverflow.com/users/179927', 'pm_score': 8, 'selected': False, 'text': "<p>To answer this not from an aesthetical but performance-oriented point of view, I've put all of the above suggestions through a <strong>benchmark</strong>. To be precise, I've considered the suggestions</p>\n\n<ul>\n<li><code>x[length(x)]</code></li>\n<li><code>mylast(x)</code>, where <code>mylast</code> is a C++ function implemented through Rcpp,</li>\n<li><code>tail(x, n=1)</code></li>\n<li><code>dplyr::last(x)</code></li>\n<li><code>x[end(x)[1]]]</code></li>\n<li><code>rev(x)[1]</code></li>\n</ul>\n\n<p>and applied them to random vectors of various sizes (10^3, 10^4, 10^5, 10^6, and 10^7). Before we look at the numbers, I think it should be clear that anything that becomes noticeably slower with greater input size (i.e., anything that is not O(1)) is not an option. Here's the code that I used:</p>\n\n<pre><code>Rcpp::cppFunction('double mylast(NumericVector x) { int n = x.size(); return x[n-1]; }')\noptions(width=100)\nfor (n in c(1e3,1e4,1e5,1e6,1e7)) {\n  x &lt;- runif(n);\n  print(microbenchmark::microbenchmark(x[length(x)],\n                                       mylast(x),\n                                       tail(x, n=1),\n                                       dplyr::last(x),\n                                       x[end(x)[1]],\n                                       rev(x)[1]))}\n</code></pre>\n\n<p>It gives me</p>\n\n<pre><code>Unit: nanoseconds\n           expr   min      lq     mean  median      uq   max neval\n   x[length(x)]   171   291.5   388.91   337.5   390.0  3233   100\n      mylast(x)  1291  1832.0  2329.11  2063.0  2276.0 19053   100\n tail(x, n = 1)  7718  9589.5 11236.27 10683.0 12149.0 32711   100\n dplyr::last(x) 16341 19049.5 22080.23 21673.0 23485.5 70047   100\n   x[end(x)[1]]  7688 10434.0 13288.05 11889.5 13166.5 78536   100\n      rev(x)[1]  7829  8951.5 10995.59  9883.0 10890.0 45763   100\nUnit: nanoseconds\n           expr   min      lq     mean  median      uq    max neval\n   x[length(x)]   204   323.0   475.76   386.5   459.5   6029   100\n      mylast(x)  1469  2102.5  2708.50  2462.0  2995.0   9723   100\n tail(x, n = 1)  7671  9504.5 12470.82 10986.5 12748.0  62320   100\n dplyr::last(x) 15703 19933.5 26352.66 22469.5 25356.5 126314   100\n   x[end(x)[1]] 13766 18800.5 27137.17 21677.5 26207.5  95982   100\n      rev(x)[1] 52785 58624.0 78640.93 60213.0 72778.0 851113   100\nUnit: nanoseconds\n           expr     min        lq       mean    median        uq     max neval\n   x[length(x)]     214     346.0     583.40     529.5     720.0    1512   100\n      mylast(x)    1393    2126.0    4872.60    4905.5    7338.0    9806   100\n tail(x, n = 1)    8343   10384.0   19558.05   18121.0   25417.0   69608   100\n dplyr::last(x)   16065   22960.0   36671.13   37212.0   48071.5   75946   100\n   x[end(x)[1]]  360176  404965.5  432528.84  424798.0  450996.0  710501   100\n      rev(x)[1] 1060547 1140149.0 1189297.38 1180997.5 1225849.0 1383479   100\nUnit: nanoseconds\n           expr     min        lq        mean    median         uq      max neval\n   x[length(x)]     327     584.0     1150.75     996.5     1652.5     3974   100\n      mylast(x)    2060    3128.5     7541.51    8899.0     9958.0    16175   100\n tail(x, n = 1)   10484   16936.0    30250.11   34030.0    39355.0    52689   100\n dplyr::last(x)   19133   47444.5    55280.09   61205.5    66312.5   105851   100\n   x[end(x)[1]] 1110956 2298408.0  3670360.45 2334753.0  4475915.0 19235341   100\n      rev(x)[1] 6536063 7969103.0 11004418.46 9973664.5 12340089.5 28447454   100\nUnit: nanoseconds\n           expr      min         lq         mean      median          uq       max neval\n   x[length(x)]      327      722.0      1644.16      1133.5      2055.5     13724   100\n      mylast(x)     1962     3727.5      9578.21      9951.5     12887.5     41773   100\n tail(x, n = 1)     9829    21038.0     36623.67     43710.0     48883.0     66289   100\n dplyr::last(x)    21832    35269.0     60523.40     63726.0     75539.5    200064   100\n   x[end(x)[1]] 21008128 23004594.5  37356132.43  30006737.0  47839917.0 105430564   100\n      rev(x)[1] 74317382 92985054.0 108618154.55 102328667.5 112443834.0 187925942   100\n</code></pre>\n\n<p>This immediately rules out anything involving <code>rev</code> or <code>end</code> since they're clearly not <code>O(1)</code> (and the resulting expressions are evaluated in a non-lazy fashion). <code>tail</code> and <code>dplyr::last</code> are not far from being <code>O(1)</code> but they're also considerably slower than <code>mylast(x)</code> and <code>x[length(x)]</code>. Since <code>mylast(x)</code> is slower than <code>x[length(x)]</code> and provides no benefits (rather, it's custom and does not handle an empty vector gracefully), I think the answer is clear: <strong>Please use <code>x[length(x)]</code></strong>.</p>\n"}, {'answer_id': 37686960, 'author': 'Enrique Pérez Herrero', 'author_id': 4678112, 'author_profile': 'https://Stackoverflow.com/users/4678112', 'pm_score': 4, 'selected': False, 'text': '<p>Package <code>data.table</code> includes <code>last</code> function</p>\n\n<pre><code>library(data.table)\nlast(c(1:10))\n# [1] 10\n</code></pre>\n'}, {'answer_id': 37687126, 'author': 'Sam Firke', 'author_id': 4470365, 'author_profile': 'https://Stackoverflow.com/users/4470365', 'pm_score': 5, 'selected': False, 'text': '<p>The <a href="https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html" rel="noreferrer">dplyr</a> package includes a function <code>last()</code>:</p>\n\n<pre><code>last(mtcars$mpg)\n# [1] 21.4\n</code></pre>\n'}, {'answer_id': 43760585, 'author': 'smoff', 'author_id': 6029286, 'author_profile': 'https://Stackoverflow.com/users/6029286', 'pm_score': 2, 'selected': False, 'text': '<p>The xts package provides a <code>last</code> function:</p>\n\n<pre><code>library(xts)\na &lt;- 1:100\nlast(a)\n[1] 100\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/77434', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14008/']