9

可能重复:
为什么 as.Date 在字符向量上慢?

data.frame使用RMySQL. 日期是作为字符引入的(似乎没有办法改变它),所以我as.Date用来将事物转换为日期。然而,由于有如此多的观察,这需要非常长的时间。有什么办法可以让这更快吗?

4

1 回答 1

21

Simon Urbanek 的fasttime库对于可解析日期时间的子集非常快:

R> now <- Sys.time()
R> now
[1] "2012-10-15 10:07:28.981 CDT"
R> fasttime::fastPOSIXct(format(now))
[1] "2012-10-15 05:07:28.980 CDT"
R> as.Date(fasttime::fastPOSIXct(format(now)))
[1] "2012-10-15"
R> 

但是,它只解析 ISO 格式并将 UTC 假定为时区。

3 1/2 年后编辑: 一些评论者似乎认为 fasttime 包很难安装。我不敢苟同。这是(再次)使用install.r它只是一个使用littler的简单包装器(并且也作为示例附带):

edd@max:~$ install.r fasttime
trying URL 'https://cran.rstudio.com/src/contrib/fasttime_1.0-1.tar.gz'
Content type 'application/x-gzip' length 2646 bytes
==================================================
downloaded 2646 bytes

* installing *source* package ‘fasttime’ ...
** package ‘fasttime’ successfully unpacked and MD5 sums checked
** libs
ccache gcc -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -O3 -Wall -pipe -pedantic -std=gnu99  -c tparse.c -o tparse.o
ccache gcc -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o fasttime.so tparse.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/fasttime/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (fasttime)

The downloaded source packages are in
        ‘/tmp/downloaded_packages’
edd@max:~$ 

如您所见,该软件包的外部依赖项为零,只有一个源文件,并且可以毫无障碍地构建。我们还可以看到,fasttime现在在 CRAN 上,而写答案时并非如此。这样一来,Windows 和 OS X 二进制文件现在确实存在于该页面上,即使您不从源代码安装,安装也会像我一样简单。

于 2012-10-15T15:08:57.163 回答