【发布时间】:2017-03-10 18:31:47
【问题描述】:
我喜欢 dplyr 的“progress_estimated”功能,但我不知道如何让进度条在 dplyr 链中工作。我在这里底部放了一个可重现的示例代码。
我有一个相当大的 data.frame,像这样:
cdatetime latitude longitude
1 2013-01-11 06:40:00 CST 49.74697 -93.30951
2 2013-01-12 15:55:00 CST 49.74697 -93.30951
3 2013-01-07 20:30:00 CST 49.74697 -93.30951
我想使用库计算每个日期的日出时间
library(dplyr)
library(StreamMetabolism)
我可以让 dplyr 的 progress_estimated 条在循环中工作,例如:
丑陋的循环(有效)
p <- progress_estimated(nrow(test))
for (i in 1:nrow(test)){
p$tick()$print()
datetime = as.POSIXct(substr(test$cdatetime[i], 1, 20), tz = "CST6CDT")
test$sunrise[i] <- sunrise.set(test$latitude[i], test$longitude[i], datetime, "CST6CDT", num.days = 1)[1,1]
}
但是如何将它嵌套在我的函数中,这样我就可以避免使用循环?
喜欢使用:
SunriseSet <- function(dataframe, timezone){
dataframe %>%
rowwise() %>%
mutate(# calculate the date-time using the correct timezone
datetime = as.POSIXct(substr(cdatetime, 1, 20), tz = timezone),
# Get the time of sunrise and sunset on this day, at the county midpoint
sunrise = sunrise.set(latitude, longitude, datetime, timezone, num.days = 1)[1,1])
}
如何在这里获得进度条?
test2 <- SunriseSet(test, "CST6CDT")
以下是一些示例数据:
test <- data.frame(cdatetime = rep("2013-01-11 06:40:00", 300),
latitude = seq(49.74697, 50.04695, 0.001),
longitude = seq(-93.30951, -93.27960, 0.0001))
【问题讨论】:
-
我觉得进度条应该也可以分组使用。例如,如果您将日期分组,则在每个组之间进行变异。
标签: r progress-bar dplyr