以下是示例数据集:
> dput(veh)
structure(list(Vehicle.ID2 = c("857-850", "857-850", "857-850",
"857-850"), svel = c(12.21277, 12.22125, 12.2362, 12.26268),
frspacing = c(10.73435, 10.64279, 10.54999, 10.45493), Local.Y = c(394.76339,
395.98552, 397.20914, 398.43541), PrecVehLocalY = c(409.70444,
410.83501, 411.96583, 413.09704), CC0 = c(4.1374232, 4.1374232,
4.1374232, 4.1374232), CC8 = c(1.75, 1.75, 1.75, 1.75), CC9 = c(1.04,
1.04, 1.04, 1.04), PrecVehLength = c(4.2067, 4.2067, 4.2067,
4.2067)), .Names = c("Vehicle.ID2", "svel", "frspacing",
"Local.Y", "PrecVehLocalY", "CC0", "CC8", "CC9", "PrecVehLength"
), class = c("tbl_df", "data.frame"), row.names = c(NA, -4L))
我将以下函数应用于数据框veh
:
apply_W99 <- function(df){
for( i in ( seq_len( nrow(df)-1 ) + 1 ) ) {
if( i <= 2L ) {
df$Un_dt_1[i] <- df$svel[i-1] * 3.6 +
3.6 * ( df$CC8[i] + ( df$CC8[i] - df$CC9[i] ) *
df$svel[i-1] * 3.6 / 80 ) * 0.1
df$Un_dt_2[i] <- 3.6 * ( df$frspacing[i-1] - df$CC0[i] ) / 0.1
} else {
df$Un_dt_1[i] <- df$Un_dt[i-1] +
3.6 * ( df$CC8[i] + ( df$CC8[i] - df$CC9[i] ) *
df$Un_dt[i-1] / 80 ) * 0.1
df$Un_dt_2[i] <- 3.6 * ( df$pred_frspacing[i-1] - df$CC0[i] ) / 0.1
}
df$Un_dt[i] <- pmin( df$Un_dt_1[i], df$Un_dt_2[i] )
if( i <= 2 ) {
df$pred_Local.Y[i] <- df$Local.Y[i-1] +
0.5 * ( ( df$Un_dt[i] + df$svel[i-1] ) / 3.6 ) * 0.1
} else {
df$pred_Local.Y[i] <- df$pred_Local.Y[i-1] +
0.5 * ( ( df$Un_dt[i] + df$Un_dt[i-1] ) / 3.6 ) * 0.1
}
df$pred_frspacing[i] <- df$PrecVehLocalY[i] - df$pred_Local.Y[i] - df$PrecVehLength[i]
}
return(df)
}
请注意,原始数据框有多个Vehicle.ID2
s。我使用tidyr
和purrr
来应用此功能。另外,我使用safely
中的purrr
函数来收集结果和错误。
library(tidyr)
foob <- veh %>%
group_by(Vehicle.ID2) %>%
nest()
library(purrr)
foos <- foob %>%
mutate(joo = map(data, safely(apply_W99)))
这很好用。现在,我想得到结果。 safely
创建一个包含result
和error
的列表。对于示例数据:
> dput(foos$joo)
list(structure(list(result = structure(list(svel = c(12.21277,
12.22125, 12.2362, 12.26268), frspacing = c(10.73435, 10.64279,
10.54999, 10.45493), Local.Y = c(394.76339, 395.98552, 397.20914,
398.43541), PrecVehLocalY = c(409.70444, 410.83501, 411.96583,
413.09704), CC0 = c(4.1374232, 4.1374232, 4.1374232, 4.1374232
), CC8 = c(1.75, 1.75, 1.75, 1.75), CC9 = c(1.04, 1.04, 1.04,
1.04), PrecVehLength = c(4.2067, 4.2067, 4.2067, 4.2067), Un_dt_1 = c(NA,
44.73644328054, 45.5093762168213, 46.2847786738341), Un_dt_2 = c(NA,
237.489364799999, 249.715278159729, 245.301888411047), Un_dt = c(NA,
44.73644328054, 45.5093762168213, 46.2847786738341), pred_Local.Y = c(NA,
395.554351295563, 396.807765455249, 398.082684273174), pred_frspacing = c(NA,
11.0739587044369, 10.9513645447513, 10.8076557268256)), .Names = c("svel",
"frspacing", "Local.Y", "PrecVehLocalY", "CC0", "CC8", "CC9",
"PrecVehLength", "Un_dt_1", "Un_dt_2", "Un_dt", "pred_Local.Y",
"pred_frspacing"), row.names = c(NA, -4L), class = c("tbl_df",
"data.frame")), error = NULL), .Names = c("result", "error")))
如何才将result
部分作为新数据框?
我尝试使用foos <- unnest(foos, map(joo, transpose()))
,但这不起作用。