我尝试使用sunriset
来计算一组lon / lat / timestamp坐标的日出时间,使用maptools中的library(maptools)
library(dplyr)
pts <- tbl_df(data.frame(
lon=c(12.08752,12.08748,12.08754,12.08760,12.08746,12.08748),
lat=c(52.11760,52.11760,52.11747,52.11755,52.11778,52.11753),
timestamp=as.POSIXct(
c("2011-08-12 02:00:56 UTC","2011-08-12 02:20:22 UTC",
"2011-08-12 02:40:15 UTC","2011-08-12 03:00:29 UTC",
"2011-08-12 03:20:26 UTC","2011-08-12 03:40:30 UTC"))
))
pts %>% mutate(sunrise=sunriset(as.matrix(lon,lat),
timestamp,POSIXct.out=T,
direction='sunrise')$time)
函数。这是一个可重复的例子。
sunriset
当我运行此代码时,我收到错误
&#34;错误:无效的下标类型&#39;关闭&#39;&#34;
我猜这意味着我没有正确地将变量传递给dplyr
。
如果我在没有pts$sunrise<-sunriset(as.matrix(select(pts,lon,lat)),
pts$timestamp, POSIXct.out=T,
direction='sunrise')$time
npm ERR! Windows_NT 10.0.10240
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\..\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "install"
npm ERR! node v4.1.1
npm ERR! npm v3.3.4
npm ERR! code ELIFECYCLE
npm ERR! bufferutil@1.1.0 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the bufferutil@1.1.0 install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the bufferutil package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls bufferutil
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Git_projects\meanjs\npm-debug.log
C:\Git_projects\meanjs>
但是,我有很多行(大约6500万),上面的方法非常慢,即使只有一小部分。我希望dplyr会更快。如果有人对什么方法可能最快有其他建议,我很乐意听到它们。
答案 0 :(得分:2)
sunr <- function(lon, lat, ts, dir='sunrise') {
# can also do matrix(c(pts$lon, pts$lat), ncol=2, byrow=TRUE) vs
# as.matrix(data.frame…
sunriset(as.matrix(data.frame(lon, lat)), ts, POSIXct.out=TRUE, direction=dir)$time
}
pts %>% mutate(sunrise = sunr(lon, lat, timestamp))
是处理它的一种方法(并且具有清洁mutate
管道的副作用)但我不确定为什么你认为它会更快。无论哪种方式,瓶颈(最有可能)是为sunriset
调用创建矩阵,这将以任何一种方式发生。
maptools
来源很容易通过,并且有一个非导出的函数maptools:::.sunrisetUTC()
可以:
".sunrisetUTC" <- function(jd, lon, lat, direction=c("sunrise", "sunset")) {
## Value: Numeric, UTC time of sunrise or sunset, in minutes from zero
## Z.
## --------------------------------------------------------------------
## Arguments: jd=julian day (real);
## lon=lat=longitude and latitude, respectively, of the observer in
## degrees;
## sunrise=logical indicating whether sunrise or sunset UTC should be
## returned.
你可以尝试在朱利安日,lon,lat&amp;它与导出函数的方向,以避免数据复制。但是,如果性能至关重要,我会使用Rcpp
编写基于this的内联矢量化C / C ++函数。