我有一个名为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:
然后我将这个大的xdf按cm_mac_address列拆分成许多小的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有一个很好的代码,可以在下面排成一行。 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>
答案 0 :(得分: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
}