如何在R中的dplyr链中添加进度条 [英] How to add progress bar inside dplyr chain in R
问题描述
我喜欢dplyr的progress_estimated功能,但我无法弄清楚如何获得进度条在dplyr链中工作。我在这里放了一个可重复的例子,代码在底部。
我有一个很大的数据框架,如下所示:
cdatetime纬度经度
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)
库(StreamMetabolism)
我可以让dplyr的progress_estimated bar在循环中工作,例如: p>
丑陋的循环(作品)
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,nu m.days = 1)[1,1]
}
但是如何嵌套它在我的功能,所以我可以避免使用循环?
喜欢使用:
SunriseSet< - function(dataframe,timezone){
$> $ p
dataframe%>%
rowwise()%>%
mutate #使用正确的时区计算日期时间
datetime = as.POSIXct(substr(cdatetime,1,20),tz = timezone),
#获取当天的日出和日落时间,在县中点
sunrise = sunrise.set(纬度,经度,日期时间,时区,num.days = 1)[1,1])$ b $ b}
如何在这里获得进度条?
test2< - SunriseSet(test,CST6CDT)
以下是一些示例数据:
test< - data.frame(cdatetime = rep(2013-01 -11 06:40:00,300),
latitude = seq(49.74697,50.04 695,0.001),
longitude = seq(-93.30951,-93.27960,0.0001))
解决方案我不喜欢我的解决方案,但它的工作原理。
print_tick_function& (x,p){
/ pre>
p $ tick()$ print()
data.frame(x)
}
SunriseSet< - function(dataframe,timezone ){
p< - progress_estimated(nrow(dataframe))
dataframe%>%
rowwise()%>%
do(print_tick_function(。,p) >%
mutate(
datetime = as.POSIXct(substr(cdatetime,1,20),tz = timezone),
sunrise = sunrise.set(纬度,经度,日期时间,时区,num.days = 1)[1,1]
)
}
test2< - SunriseSet(test,CST6CDT)
I like dplyr's "progress_estimated" function but I can't figure out how to get a progress bar to work inside a dplyr chain. I've put a reproducible example with code at the bottom here.
I have a pretty big data.frame like this:
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
and I'd like to calculate sunrise times for each date, using the libraries
library(dplyr) library(StreamMetabolism)
I can get dplyr's progress_estimated bar to work within a loop, e.g.:
Ugly loop (works)
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] }
but how can I nest it in my function, so I can avoid using a loop?
Prefer to use:
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]) }
How to get a progress bar here?
test2 <- SunriseSet(test, "CST6CDT")
Here's some example data:
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))
解决方案I dont really like my solution but it works.
print_tick_function <- function(x, p) { p$tick()$print() data.frame(x) } SunriseSet <- function(dataframe, timezone){ p <- progress_estimated(nrow(dataframe)) dataframe %>% rowwise() %>% do(print_tick_function(.,p)) %>% mutate( datetime = as.POSIXct(substr(cdatetime, 1, 20), tz = timezone), sunrise = sunrise.set(latitude, longitude, datetime, timezone, num.days = 1)[1,1] ) } test2 <- SunriseSet(test, "CST6CDT")
这篇关于如何在R中的dplyr链中添加进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!