访问R data.frame中倒数第二个值的最佳方法是什么? [英] What is the best way to access the second to last value in an R data.frame?
本文介绍了访问R data.frame中倒数第二个值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个名为SLV的data.frame,并且应用了tail()函数。我会得到这个:
Suppose I have a data.frame called SLV and I apply the tail()function. I will get this:
> tail(SLV)
SLV.Open SLV.High SLV.Low SLV.Close SLV.Volume SLV.Adjusted
2010-12-06 28.99 29.64 28.88 29.51 57561800 29.51
2010-12-07 29.95 30.00 28.03 28.08 69143800 28.08
2010-12-08 28.33 28.46 27.34 27.70 58203800 27.70
2010-12-09 28.10 28.36 27.83 28.03 36759200 28.03
2010-12-10 27.80 28.11 27.38 27.98 30602700 27.98
2010-12-13 28.84 29.04 28.59 28.87 25901800 28.87
tail ()默认为最后6个值,但是很容易获得最后1个值。
tail() defaults to the last 6 values, but it's easy to get just the last 1 value.
>tail(SLV, n=1)
SLV.Open SLV.High SLV.Low SLV.Close SLV.Volume SLV.Adjusted
2010-12-13 28.84 29.04 28.59 28.87 25901800 28.87
但是将倒数第二天返回到最后一天的最佳方法是什么?在我们的SLV示例中,该行的日期为2010-12-10。
But what is the best way to return the second to last day? In our SLV example, it would be the line that is dated 2010-12-10.
推荐答案
head(tail(SLV, n=2), n=1)
或
SLV[nrow(SLV)-1,]
会做到这一点。
这篇关于访问R data.frame中倒数第二个值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文