0

我有很多时间数据(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
4

2 回答 2

1

目前尚不清楚您想做什么。您可以从读取数据开始。因为它是不规则的时间序列,并且包含一个因子变量(第一组),所以你不能使用像zooor这样的方便的包xts,因为它们需要一个矩阵作为结构。但是你可以freaddata.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
于 2013-03-20T07:15:15.600 回答
0

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

data_ID_P001 <- ddply(data_ID_P001, .(time_recorded, 联合), 总结, average_x_pos = mean(x_pos), average_y_pos = mean(y_pos), average_z_pos = mean(z_pos))

于 2013-10-20T01:06:01.020 回答