1

我有一个名为 probe03Seq 的 xdf 文件,其中包含事件列表。

Variable information: 
Var 1: cm_mac_address       55718 factor levels: 
Var 2: time, Type: POSIXct
Var 3: status       3 factor levels:
Var 4: duration_disc       10 factor levels:
Var 5: down_power_disc       10 factor levels: 
Var 6: down_snr_disc       10 factor levels:
Var 7: down_speed_disc       10 factor levels
Var 8: latency_disc       1 factor levels:
Var 9: up_power_disc       10 factor levels:
Var 10: up_speed_disc       10 factor levels:
Var 11: Sequence       34777 factor levels:

然后我按列 cm_mac_address 将这个大 xdf 拆分为许多小的 xdf,并按时间对它们进行排序。

nocSplit <- rxSplit(inData = "probe03Seq.xdf",
                 outFilesBase = file.path(tempdir(), "MACAddress"),
                 splitByFactor = "cm_mac_address")

mclapply(nocSplit, FUN = function(xdf) {

rxSort(inData = xdf,
       outFile = xdf,
       sortByVars = "time",
       overwrite = TRUE)
})

我现在正试图弄清楚如何在这些小 xdf 中创建一个新变量。我希望能够设置窗口大小并能够在序列列中创建先前值的数组。因此,例如,如果我每小时进行一次观察,窗口大小为 10 小时,我希望看到类似下面的 SequenceList 列。

window.size = 10 hrs

time Sequence SequenceList
1      6       NA
2      5       NA
3      7       NA
4      8       NA
5      4       NA
6      2       NA
7     10       NA
8      9       NA
9      4       NA
10     6       {6,5,7,8,4,2,10,9,4,6}
11     4       {5,7,8,4,2,10,9,4,6,4}
12     3       {7,8,4,2,10,9,4,6,4,3}
13     8       {8,4,2,10,9,4,6,4,3,8}
14     3       {4,2,10,9,4,6,4,3,8,3}
15     9       {2,10,9,4,6,4,3,8,3,9}
16     1       {10,9,4,6,4,3,8,3,9,1}
17     7       {9,4,6,4,3,8,3,9,1,7}
18     3       {4,6,4,3,8,3,9,1,7,3}
19     8       {6,4,3,8,3,9,1,7,3,8}
20     10      {4,3,8,3,9,1,7,3,8,10}

Azure 团队的 Matt Parker 有一个很好的代码,用于滞后 1 行。 https://gist.github.com/mmparker/8aca803eae5410875a21

lagVar <- function(dataList) { 

 if(.rxStartRow == 1) {
    dataList[[newName]] <- c(NA, dataList[[varToLag]][-.rxNumRows]) 
} else {
    dataList[[newName]] <- c(.rxGet("lastValue"),
                             dataList[[varToLag]][-.rxNumRows]) 
  }

.rxSet("lastValue", dataList[[varToLag]][.rxNumRows])

dataList

}


lapply(djiaSplit, FUN = function(xdf) {

rxDataStep(inData = xdf, 
           outFile = xdf,
           transformObjects = list(
               varToLag = "Open", 
               newName = "previousOpen"), 
           transformFunc = lagVar,
           # append = "cols",
           overwrite = TRUE)

})

我认为可以再次使用在 mclapply 中使用自定义函数包装 rxDataStep 的相同方法。我只是在想出这个功能时遇到了麻烦。任何帮助,将不胜感激!目前我有这个代码

我想出了一个适用于常规数据框的函数,

set.seed(100)
mydf<-data.frame(time=(1:1000),event = sample(1:10,10000,replace=TRUE))

w=10
for (i in 1:nrow(mydf)){
  if(i<=w){
    mydf$eventList[i] = NA
    } 
 else {
      mydf$eventList[i] = list(mydf$event[c((i-w):i)])
      }
}

但是,当我修改它以使用 xdf 文件时,我得到一个错误。

lagVarWindow <- function(dataList) { 

for (i in 1:.rxNumRows){
if(i<=window.size){
 dataList[[newName]][i] = NA
} 
else {
 dataList[[newName]][i] = list(dataList[[varToLag]][c((i-window.size):i)])
}
}

dataList

}


mclapply(nocSplit, FUN = function(xdf) {

rxDataStep(inData = xdf, 
           outFile = xdf,
transformObjects = list(
window.size = 10,
              varToLag = "Sequence", 
              newName = "Sequence2"),
           transformFunc = lagVarWindow,
# append = "cols",
           overwrite = TRUE)

})

Error in doTryCatch(return(expr), name, parentenv, handler) : 
Found list tag in the middle of data: '<list=Sequence2&2190:1>
4

1 回答 1

0

我能够通过将错误包装在 paste() 函数中来修复错误

lagVarWindow <- function(dataList) { 

for (i in 1:.rxNumRows){
  if(i<=window.size){
                    dataList[[newName]][i] = NA
                    } 
  else {
       dataList[[newName]][i] = paste(list(dataList[[varToLag]][c((i-window.size):i)]))
       }
}

dataList

}
于 2016-05-22T17:38:03.563 回答