如何访问向量中的最后一个值? [英] How to access the last value in a vector?
本文介绍了如何访问向量中的最后一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个向量嵌套在一个或两个级别的数据框中。是否有一种快速而又肮脏的方法来访问最后一个值,而不使用 length()
函数? PERL的 $#
特殊变量?
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[$#]
而不是
dat$vec1$vec2[length(dat$vec1$vec2)]
推荐答案
我使用尾巴
函数:
tail(vector, n=1)
tail
的优点是它也适用于数据帧,与 x不同[length(x)]
成语。
The nice thing with tail
is that it works on dataframes too, unlike the x[length(x)]
idiom.
这篇关于如何访问向量中的最后一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文