简体   繁体   English

在R中处理时间序列数据

[英]Manipulating time series data in R

What would be a good way to manipulate the following type of times series data in R : R操作以下类型的时间序列数据的好方法是什么:

username;variable;2012-01-01;2012-01-15;2012-02-01;2012-03-01;2012-04-01;2012-05-01;2012-07-02 
user1;var1;5;5;5;5;6;6;6
user1;var2;0;0;1;0;0;1;1
user1;var3;9;9;9;9;9;9;9
user2;var1;4;4;4;4;4;6;6
user2;var2;0;0;1;1;1;1;1
user2;var3;4;4;4;9;9;9;9

The data contains a set of time series for each monitored user. 数据包含每个受监视用户的一组时间序列。 My goal is to have the data in such a format that I can easily make queries upon this data set for "deltas". 我的目标是使数据具有某种格式,以便我可以轻松地对此数据集查询“增量”。 That is, from a certain point in time I can look back and compute how long ago a certain variable changed and also get the original value and the new value from this query. 也就是说,从某个时间点开始,我可以回顾并计算某个变量在多久之前发生更改,并且还可以从该查询中获取原始值和新值。

A function that would simply take a date and variable name as an argument would be perfect, eg, fun(2012-07-02, var1) , fun(2012-02-17, var1) or fun(2014-09-02, var1) would return four columns: username,original_value;new_value;days_since_change . 仅将日期和变量名作为参数的函数将是完美的,例如fun(2012-07-02, var1)fun(2012-02-17, var1)fun(2014-09-02, var1)将返回四列: username,original_value;new_value;days_since_change

Are there R packages or pieces of code that would be able to do something similar? 是否有R包或代码段可以执行类似的操作?

Here's how to transform your table in a easy to work with format. 这是如何以易于使用的格式转换表格的方法。 the trick to to use the reshape2 package and melt your data. 使用reshape2软件包并melt数据的技巧。

my.table <-read.table(text="username;variable;2012-01-01;2012-01-15;2012-02-01;2012-03-01;2012-04-01;2012-05-01;2012-07-02
user1;var1;5;5;5;5;6;6;6
user1;var2;0;0;1;0;0;1;1
user1;var3;9;9;9;9;9;9;9
user2;var1;4;4;4;4;4;6;6
user2;var2;0;0;1;1;1;1;1
user2;var3;4;4;4;9;9;9;9",sep=";", header=TRUE)

library(reshape2)
res <-melt(my.table,id.vars=c("username","variable") )    #melt on the first two columns
colnames(res)[3] <-"Date"
res$Date <-as.Date(res$Date,format="X%Y.%m.%d")           #transform into date format

out <-res[res$username=="user1" & res$variable=="var1",]  #request user1 and var1
out

   username variable       Date value
1     user1     var1 2012-01-01     5
7     user1     var1 2012-01-15     5
13    user1     var1 2012-02-01     5
19    user1     var1 2012-03-01     5
25    user1     var1 2012-04-01     6
31    user1     var1 2012-05-01     6
37    user1     var1 2012-07-02     6

I do not understand what you want when you say original_value;new_value;days_since_change but with data extracted in such a way, I'm sure you can figure it out. 当您说original_value;new_value;days_since_change时,我不明白您想要什么original_value;new_value;days_since_change但是通过这种方式提取的数据,我相信您可以弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM