1

我有一个似乎很简单但我无法解决的问题。我有一个 R 数据框,它由单列数据点组成,如下所示。我想子集到一个新的数据帧中,其中包含基于先前数据点值的数据点。

因此,在下面,例如,我想对先前值大于 0.04 的所有行进行子集化。任何想法,将不胜感激。谢谢你。

         Price
[1,] -0.006666667
[2,]  0.040268456
[3,]  0.051612903
[4,] -0.006134969
[5,]  0.006172840
[6,]  0.006134969
[7,]  0.030487805
4

2 回答 2

2

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 

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.

于 2013-04-03T04:53:23.743 回答
2

像这样:

x[c(FALSE, head(x$Price, -1) > 0.04), , drop = FALSE]

(从您的印刷品看来,您的对象可能是矩阵,而不是 data.frame。如果是这种情况,请替换x$Pricex[, "Price"]。)

于 2013-04-03T00:40:07.960 回答