R将数据帧转换为手动时间序列

时间:2016-09-18 17:35:06

标签: r time-series

我在R

中有这个结构
 Type tpType = Type.GetTypeFromProgID("thirdPty.Application");
 dynamic comObject = Activator.CreateInstance(tpType);

 try
 {
            bool success = comObject.LoadDatabase(dbPath);

            if (success)
            {
                var newTp = comObject.Open(inputFile);

                newTp.Run(runType);

                while (newTp.IsBusy)
                {
                    // wait for process to finish
                    Thread.Sleep(500);
                }

                newAb.Export(outputFolder + "\\output" + " " + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".csv");

                newAb.Close();
            }
 }

我想要像这样的回报

DATA = read.csv("Anomaly.csv")
DATA

   col1       col2   
 57.17339 0.00064822 
 52.90232 0.00064386 
 46.20606 0.00064144 
 61.23981 0.00064216 

1 个答案:

答案 0 :(得分:0)

我们可以使用shift中的data.table进行尝试。转换' data.frame'到' data.table' (setDT(DATA)),循环遍历Data.table(lapply(.SD, ..), use the移位specifying the n as 0:2. By default the移位has类型`的子集作为& #34;延迟"选项。

library(data.table)
dt <- setDT(DATA)[, unlist(lapply(.SD, shift, n = 0:2), recursive=FALSE)]

如果我们需要订购列

dt[, order(sub("\\D+\\d", "", colnames(dt))), with = FALSE]
#     col11      col21    col12      col22    col13      col23
#1: 57.17339 0.00064822       NA         NA       NA         NA
#2: 52.90232 0.00064386 57.17339 0.00064822       NA         NA
#3: 46.20606 0.00064144 52.90232 0.00064386 57.17339 0.00064822
#4: 61.23981 0.00064216 46.20606 0.00064144 52.90232 0.00064386

或者shift也可以直接应用于Data.table的子集。

setDT(DATA)[, shift(.SD, n = 0:2)]