我正在使用库中的函数来应用函数(来自map
库),如下所示: purrr
segmented
segmented
library(purrr)
library(dplyr)
library(segmented)
# Data frame is nested to create list column
by_veh28_101 <- df101 %>%
filter(LCType=="CFonly", Lane %in% c(1,2,3)) %>%
group_by(Vehicle.ID2) %>%
nest() %>%
ungroup()
# Functions:
segf2 <- function(df){
try(segmented(lm(svel ~ Time, data=df), seg.Z = ~Time,
psi = list(Time = df$Time[which(df$dssvel != 0)]),
control = seg.control(seed=2)),
silent=TRUE)
}
segf2p <- function(df){
try(segmented(lm(PrecVehVel ~ Time, data=df), seg.Z = ~Time,
psi = list(Time = df$Time[which(df$dspsvel != 0)]),
control = seg.control(seed=2)),
silent=TRUE)
}
# map function:
models8_101 <- by_veh28_101 %>%
mutate(segs = map(data, segf2),
segsp = map(data, segf2p))
该对象by_veh28_101
包含 2457 tibbles
。最后一步,map
使用函数,需要 16 分钟才能完成。有什么办法可以让这更快吗?