rollapply

时间:2015-11-09 10:37:45

标签: r

我正在尝试使用rollapply在某些数据列上传递一个窗口。如果在函数(JRip)中满足条件,那么我希望输出数据帧的每一行显示预测的Behaviour

以下是目前的代码:

library(zoo)
# This is the matrix for the results of the function
Predicted <- matrix() # matrix to store model results



Out <- structure(list(latitude_position_1 = c("91.1227553646932", "91.1227518740347", 
"91.1227483833762", "91.1227483833762", "91.1227483833762",   "91.1227483833762"
), longtude_position_1 = c("-6.23305421507455", "-6.2330489790868", 
"-6.23304374309904", "-6.23303850711129", "-6.23304374309904", 
"-6.2330489790868"), latitude_position_2 = c("91.1227518740347", 
"91.1227483833762", "91.1227483833762", "91.1227483833762", "91.1227483833762", 
"91.1227483833762"), longtude_position_2 = c("-6.2330489790868", 
"-6.23304374309904", "-6.23303850711129", "-6.23304374309904", 
"-6.2330489790868", "-6.2330559604038"), Distance = c("0.400505694328303", 
"0.400505709727528", "0.333912980750125", "0.333912980750125", 
"0.333912980750125", "0.445217307591311"), Speed = c("0.03", 
"0.155", "0.155", "0.09", "0.275", "0.275"), Acceleration = c("0.012", 
"0.038", "-0.038", "0.012", "0.062", "-0.062"), Absolute_Heading =     c("236.306305752159", 
"236.306307393042", "270.000002484092", "89.9999975159082", "89.9999975159082", 
"89.9999966878713"), Relative_Heading = c("-236.306305752159", 
"-1.64088351084501e-06", "-33.6936950910497", "180.000004968184", 
"0", "8.28036945677013e-07"), Movement = c("left", "forward", 
"forward", "non-moving", "non-moving", "non-moving"), Changes = c("0", 
"1", "0", "1", "0", "0"), Transition = c("****", "****", "****", 
"Transition", "****", "****")), .Names = c("latitude_position_1", 
"longtude_position_1", "latitude_position_2", "longtude_position_2", 
"Distance", "Speed", "Acceleration", "Absolute_Heading", "Relative_Heading", 
"Movement", "Changes", "Transition"), row.names = c("0.1", "0.2", 
"0.3", "0.4", "0.5", "0.6"), class = "data.frame")



# Here are the columns of the dataframe I'd like to run the window over
Dist_Out <- (Out[, 5])
Speed_Out <- (Out[, 6])
Acceleration_Out <- (Out[, 7])
Absolute_Heading_Out <- (Out[, 8])
Relative_Heading_Out <- (Out[, 9])
Movement_Out <- (Out[, 10])
Changes_Out <- (Out[, 11])

# this is the object for the function
Behaviour <- JRip(Dist_Out, Speed_Out, Acceleration_Out, Absolute_Heading_Out, Relative_Heading_Out, Movement_Out, Changes_Out)

rollapply(Out, width = 2, FUN = Behaviour, by = 1, ascending = FALSE, by.column = FALSE, align = c("center"))


# this is the function
JRip <- function(Dist_Out, Speed_Out, Acceleration_Out, Absolute_Heading_Out, Relative_Heading_Out, Movement_Out, Changes_Out)
{
  # walking
  if (max(Speed_Out) >= 2.45)  Behaviour <- "walking"  else Behaviour <- "resting"

  return (Behaviour)
}


# output 'behaviour' as dataframe.
Predicted <- as.data.frame(Behaviour)

但是,我现在收到此错误:

Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'resting' of mode 'function' was not found

这个函数还处于起步阶段,因为我想在添加函数的所有其他元素之前确保它有效。该函数也使用source调用,因此不会出现在上面的代码中。

以下是一些“输出”输入:

latitude_position_1 longtude_position_1 latitude_position_2 longtude_position_2          Distance Speed Acceleration Absolute_Heading       Relative_Heading   Movement Changes Transition
1     91.1227553646932   -6.23305421507455    91.1227518740347    -6.2330489790868 0.400505694328303  0.03        0.012 236.306305752159     -236.306305752159       left       0       ****
2     91.1227518740347    -6.2330489790868    91.1227483833762   -6.23304374309904 0.400505709727528 0.155        0.038 236.306307393042 -1.64088351084501e-06    forward       1       ****
3     91.1227483833762   -6.23304374309904    91.1227483833762   -6.23303850711129 0.333912980750125 0.155       -0.038 270.000002484092     -33.6936950910497    forward       0       ****
4     91.1227483833762   -6.23303850711129    91.1227483833762   -6.23304374309904 0.333912980750125  0.09        0.012 89.9999975159082      180.000004968184 non-moving       1 Transition
5     91.1227483833762   -6.23304374309904    91.1227483833762    -6.2330489790868 0.333912980750125 0.275        0.062 89.9999975159082                     0 non-moving       0       ****
6     91.1227483833762    -6.2330489790868    91.1227483833762    -6.2330559604038 0.445217307591311 0.275       -0.062 89.9999966878713    8.28036945677013e-07 non-moving       0       ****
7     91.1227483833762    -6.2330559604038    91.1227431473884   -6.23304548842829 0.745675662288711  0.12            0 243.431811475523     -153.431814787652       left       1       ****
8     91.1227431473884   -6.23304548842829    91.1227448927177   -6.23304374309904 0.156892934292016 0.275        0.062 315.003927795227     -71.5721163197041 non-moving       1       ****
9     91.1227448927177   -6.23304374309904    91.1227536193639   -6.23306468705007  1.44555679702924  0.31       -0.048 67.3773348964212      247.626592898806      right       1       ****
10    91.1227536193639   -6.23306468705007    91.1227536193639   -6.23306643237932 0.111304317308227 0.155       -0.014 89.9999991716402     -22.6226642752191    forward       1       ****
11    91.1227536193639   -6.23306643237932    91.1227588553517   -6.23306643237932 0.331723741058604 0.155        0.014                0      89.9999991716402 non-moving       1       ****
12    91.1227588553517   -6.23306643237932    91.1227623460102   -6.23306643237932  0.22114916222707  0.19            0                0                     0 non-moving       0       ****
13    91.1227623460102   -6.23306643237932    91.1227606006809   -6.23306468705007 0.156892912024679 0.405        0.086 224.996065562981     -224.996065562981 non-moving       0       ****
14    91.1227606006809   -6.23306468705007    91.1227693273272    -6.2330559604038 0.784464537327535 0.465       -0.062  315.00394101503     -90.0078754520491       left       1       ****
15    91.1227693273272    -6.2330559604038    91.1227763086442   -6.23305945106231 0.495158944897039  0.31            0  26.561897224065      288.442043790965      right       1       ****
16    91.1227763086442   -6.23305945106231    91.1227763086442   -6.23306468705007 0.333912827211437 0.215       -0.038 89.9999975116924     -63.4381002876273       left       1       ****
17    91.1227763086442   -6.23306468705007    91.1227728179857   -6.23306992303782 0.400505598712409 0.155        0.014 123.693703315054     -33.6937058033612    forward       1       ****

我需要运行一个宽度为'32'的窗口,以便立即将函数传递给所有这些列。例如:

“如果前32行的速度> = 2.45,则将所有行标记为”行走“,然后以32个为一组继续浏览数据集。

我希望这有助于解决我遇到的错误。谢谢

0 个答案:

没有答案