如何访问向量中的最后一个值?
假设我有一个嵌套在具有一层或两层的数据框中的向量。 有没有一种快速而肮脏的方法来访问最后一个值,而不使用 length()
函数? PERL 的 $#
特殊变量?
所以我想要类似的东西:
dat$vec1$vec2[$#]
而不是:
dat$vec1$vec2[length(dat$vec1$vec2)]
Suppose I have a vector that is nested in a dataframe with 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)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我使用
tail
函数:tail
的好处是它也适用于数据帧,这与x[length(x)]
习惯用法不同。I use the
tail
function:The nice thing with
tail
is that it works on dataframes too, unlike thex[length(x)]
idiom.为了回答这个问题,不是从美观的角度,而是以性能为导向的角度,我将上述所有建议都通过了一个基准。 准确地说,我已经考虑了建议
x[length(x)]
mylast(x)
,其中mylast
是实现的 C++ 函数通过 Rcpp,tail(x, n=1)
dplyr::last(x)
x[end(x)[1]]]
rev(x)[1]
并将它们应用于各种大小(10^3、10^4、10^5、10^6 和 10^7)的随机向量。 在我们看这些数字之前,我认为应该清楚的是,任何随着输入大小的增加而明显变慢的东西(即任何不是 O(1) 的东西)都不是一个选择。 这是我使用的代码:
它给了我
这立即排除了涉及
rev
或end
的任何内容,因为它们显然不是O(1)
(并且结果表达式以非惰性方式求值)。tail
和dplyr::last
距离O(1)
不远,但它们也比mylast(x )
和x[length(x)]
。 由于mylast(x)
比x[length(x)]
慢并且没有任何好处(相反,它是自定义的并且不能优雅地处理空向量),我认为答案很明确:请使用x[length(x)]
。To answer this not from an aesthetical but performance-oriented point of view, I've put all of the above suggestions through a benchmark. To be precise, I've considered the suggestions
x[length(x)]
mylast(x)
, wheremylast
is a C++ function implemented through Rcpp,tail(x, n=1)
dplyr::last(x)
x[end(x)[1]]]
rev(x)[1]
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:
It gives me
This immediately rules out anything involving
rev
orend
since they're clearly notO(1)
(and the resulting expressions are evaluated in a non-lazy fashion).tail
anddplyr::last
are not far from beingO(1)
but they're also considerably slower thanmylast(x)
andx[length(x)]
. Sincemylast(x)
is slower thanx[length(x)]
and provides no benefits (rather, it's custom and does not handle an empty vector gracefully), I think the answer is clear: Please usex[length(x)]
.如果您正在寻找像 Python 的 x[-1] 表示法一样好的东西,我认为您不走运。 标准的习惯用法是,
但编写一个函数来执行此操作很容易:
R 中缺少的这个功能也让我烦恼!
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
but it's easy enough to write a function to do this:
This missing feature in R annoys me too!
结合 lindelof 的 和 Gregg Lind 的< /a> 想法:
在提示符下工作,我通常省略
n=
,即tail(x, 1)
。与
pastecs
包中的last
不同,head
和tail
(来自utils
)可以工作不仅在向量上,而且在数据帧等上,并且还可以返回数据“没有第一个/最后一个元素”,例如(请注意,您必须使用
head
这个,而不是tail
。)Combining lindelof's and Gregg Lind's ideas:
Working at the prompt, I usually omit the
n=
, i.e.tail(x, 1)
.Unlike
last
from thepastecs
package,head
andtail
(fromutils
) work not only on vectors but also on data frames etc., and also can return data "without first/last n elements", e.g.(Note that you have to use
head
for this, instead oftail
.)dplyr 包包含一个函数
last()< /代码>:
The dplyr package includes a function
last()
:我只是使用以下代码在具有 663,552 行的数据帧上对这两种方法进行了基准测试:
因此
,假设您正在使用向量,则访问长度位置的速度要快得多。
I just benchmarked these two approaches on data frame with 663,552 rows using the following code:
and
So, assuming you're working with vectors, accessing the length position is significantly faster.
另一种方法是取反转向量的第一个元素:
Another way is to take the first element of the reversed vector:
我有另一种方法来查找向量中的最后一个元素。
假设向量是
a
。就这样吧!
I have another method for finding the last element in a vector.
Say the vector is
a
.There you go!
data.table
包包含last
函数Package
data.table
includeslast
function关于什么
Whats about
xts 包提供了一个
last
函数:The xts package provides a
last
function:自
purrr
1.0.0 起,pluck
现在接受负整数从右侧索引:As of
purrr
1.0.0,pluck
now accepts negative integers to index from the right: