R中复杂的基于时间的子集

时间:2013-03-20 05:38:25

标签: r

我有大量的时间数据(YYYY / MM / DD HH:MM:SS.SSS)以不规则的数千秒间隔存储。在每个时间段,有十个空间测量值(X,Y和Z值)。

我想要的是获取数据的子集,例如每半秒(或几分之一秒)的第一组十个空间测量。

我对R很新,所以任何帮助都会非常感激!

以下是2个测量时间的示例:

2012/09/21 14:59:07:712,A,0.036,0.224,0.814
2012/09/21 14:59:07:712,B,0.042,0.057,0.934
2012/09/21 14:59:07:712,C,-0.104,0.008,0.930
2012/09/21 14:59:07:712,D,0.158,0.001,0.914
2012/09/21 14:59:07:712,E,-0.208,-0.168,0.778
2012/09/21 14:59:07:712,F,-0.185,0.087,0.748
2012/09/21 14:59:07:712,G,-0.176,0.155,0.738
2012/09/21 14:59:07:712,H,0.236,-0.171,0.790
2012/09/21 14:59:07:712,I,0.244,0.076,0.732
2012/09/21 14:59:07:712,J,0.248,0.137, 0.722
2012/09/21 14:59:07:848,A,0.036,0.224,0.814
2012/09/21 14:59:07:848,B,0.042,0.057,0.934
2012/09/21 14:59:07:848,C,-0.104,0.008,0.930
2012/09/21 14:59:07:848,D,0.158,0.001,0.914
2012/09/21 14:59:07:848,E,-0.208,-0.168,0.778
2012/09/21 14:59:07:848,F,-0.185,0.087,0.748
2012/09/21 14:59:07:848,G,-0.176,0.155,0.738
2012/09/21 14:59:07:848,H,0.236,-0.171,0.790
2012/09/21 14:59:07:848,I,0.244,0.076,0.732
2012/09/21 14:59:07:848,J,0.248,0.137, 0.722

2 个答案:

答案 0 :(得分:1)

目前尚不清楚你想做什么。您可以从阅读数据开始。由于它是irregulat时间序列,并且包含因子变量(组1),因此您不能使用像zooxts这样的方便包,因为它们需要矩阵作为结构。但您可以使用fread包中的data.table

library(data.table)
dat <- fread('2012/09/21 14:59:07:712,A,0.036,0.224,0.814
2012/09/21 14:59:07:712,B,0.042,0.057,0.934
2012/09/21 14:59:07:712,C,-0.104,0.008,0.930
2012/09/21 14:59:07:712,D,0.158,0.001,0.914
2012/09/21 14:59:07:712,E,-0.208,-0.168,0.778
2012/09/21 14:59:07:712,F,-0.185,0.087,0.748
2012/09/21 14:59:07:712,G,-0.176,0.155,0.738
2012/09/21 14:59:07:712,H,0.236,-0.171,0.790
2012/09/21 14:59:07:712,I,0.244,0.076,0.732
2012/09/21 14:59:07:712,J,0.248,0.137, 0.722
2012/09/21 14:59:07:848,A,0.036,0.224,0.814
2012/09/21 14:59:07:848,B,0.042,0.057,0.934
2012/09/21 14:59:07:848,C,-0.104,0.008,0.930
2012/09/21 14:59:07:848,D,0.158,0.001,0.914
2012/09/21 14:59:07:848,E,-0.208,-0.168,0.778
2012/09/21 14:59:07:848,F,-0.185,0.087,0.748
2012/09/21 14:59:07:848,G,-0.176,0.155,0.738
2012/09/21 14:59:07:848,H,0.236,-0.171,0.790
2012/09/21 14:59:07:848,I,0.244,0.076,0.732
2012/09/21 14:59:07:848,J,0.248,0.137, 0.722',header=FALSE)

现在你可以玩你的结构了。例如,要获得前5个组,请执行以下操作:

 dat[V2 %in% LETTERS[1:5],]
                         V1 V2     V3     V4    V5
 1: 2012/09/21 14:59:07:712  A  0.036  0.224 0.814
 2: 2012/09/21 14:59:07:712  B  0.042  0.057 0.934
 3: 2012/09/21 14:59:07:712  C -0.104  0.008 0.930
 4: 2012/09/21 14:59:07:712  D  0.158  0.001 0.914
 5: 2012/09/21 14:59:07:712  E -0.208 -0.168 0.778
 6: 2012/09/21 14:59:07:848  A  0.036  0.224 0.814
 7: 2012/09/21 14:59:07:848  B  0.042  0.057 0.934
 8: 2012/09/21 14:59:07:848  C -0.104  0.008 0.930
 9: 2012/09/21 14:59:07:848  D  0.158  0.001 0.914
10: 2012/09/21 14:59:07:848  E -0.208 -0.168 0.778

答案 1 :(得分:0)

这是我提出的能够解决问题的解决方案(唯一的缺点是它以1秒的间隔创建移动平均线):

data_ID_P001&lt; - ddply(data_ID_P001,。(time_recorded,joint),     总结,     average_x_pos = mean(x_pos),     average_y_pos = mean(y_pos),     average_z_pos = mean(z_pos))