如何根据前一行的值选择 R 数据框中的行 [英] How to select rows in an R data frame based on values of previous rows
问题描述
我有一个似乎很简单的问题,但我无法解决.我有一个 R 数据框,它由一列数据点组成,如下所示.我想子集到一个新的数据框,其中包含基于先前数据点的值的数据点.
I have what seems to be a simple problem which I haven't been able to solve. I have an R data frame which consists of a single column of data points, as show below. I would like to subset into a new data frame which contains data points based on value of previous data points.
所以在下面,例如,我想对前一个值大于 .04 的所有行进行子集化.任何想法,将不胜感激.谢谢.
So below, I would for example like to subset all the rows where the previous value was greater than .04. Any ideas would be appreciated. Thank you.
Price
[1,] -0.006666667
[2,] 0.040268456
[3,] 0.051612903
[4,] -0.006134969
[5,] 0.006172840
[6,] 0.006134969
[7,] 0.030487805
推荐答案
这些类型的操作可以通过使用时间序列表示直接模仿我们的思维过程来完成.这也有一个优点,它现在在这样的表示中,并且也将促进进一步的计算.假设 DF
是数据帧.将其转换为 zoo 对象 z
,然后提取这些组件z
的滞后超过 0.04
:
These types of manipulations can be done in a way which directly mimics our thought process by using a time series representation. This also has the advantage that its now in such a representation and that will facilitate further computations as well. Suppose DF
is the data frame. Convert it to a zoo object z
and then extract those components of z
whose lag exceeds 0.04
:
> library(zoo)
> z <- zoo(DF$Price)
> z[lag(z, -1) > 0.04]
3 4
0.051612903 -0.006134969
如果 result
是最后一行代码的值,则 time(result)
给出时间(3
和 4
在上面的例子中)和 coredata(result)
给出数据值.
If result
is the value of the last line of code then time(result)
gives the times (3
and 4
in the above example) and coredata(result)
gives the data values.
这篇关于如何根据前一行的值选择 R 数据框中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!