c++ - R style subset/replace in Rcpp -
in r have loop runs through data frame of people coming , leaving site , assigns them parking lot until leave when releases spot.
activity = structure(list(id = c(1, 2, 3, 3, 1, 2, 3, 2, 3, 1, 2, 1), lot = structure(c(1l, 3l, 6l, 6l, 1l, 3l, 2l, 5l, 2l, 4l, 5l, 4l), .label = c("a", "a", "c", "d", "f", "z"), class = "factor"), time = c(1, 3, 6, 7, 8, 9, 10, 12, 13, 14, 15, 21), dir = c(1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1), here = c("no","no","no","no","no","no","no","no","no","no","no","no")), class = "data.frame", row.names = c(na, -12l), .names = c("id", "lot", "time", "dir","here")) lots = structure(c(30, 175, 170, 160, 300, 300, 35, 160, 85, 400, 200, 110, 60, 130, 100, 80, 1000, 320, 330, 350, 320, 250, 250, 370, 20, 40, 0, 140, 185, 200, 185, 120, 55, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .dim = c(34l, 2l), .dimnames = list(c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i"), c("capacity", "utilization"))) for(i in 1:nrow(activity)){ id = activity$id[i] l = activity$lot[i] d = activity$dir[i] t = activity$time[i] lots[l,"utilization"] = lots[l,"utilization"] + d if(d == -1) activity$here[activity$id == id & activity$time <= t] = "no" }
what want determine is, in rcpp
there way r style subset , replace? e.g., lots[l,"utilization"] = ...
, activity$here[activity$id == id & activity$time <= t] = ...
. i'm wanting able load in 3 matrices, master record table, matrix manages lots , matrix manages current locations (in example know lot already, in practice need determine park). have functioning code takes ~160 seconds run , i'm trying learn how leverage rcpp
make faster.
Comments
Post a Comment