我正试图将时间从午夜转换为秒.我很难从chron包中获取times()函数.这是我使用它的方式:
> library(chron)
> 24 * 24 * 60 * (times(50))
Error in 24 * 24 * 60 * (times(50)) :
non-numeric argument to binary operator
>
>
> library(chron)
> 24 * 24 * 60 times(5000)
Error: unexpected symbol in "24 * 24 * 60 times"
Run Code Online (Sandbox Code Playgroud)
有什么建议?
更新:
> sessionInfo()
R version 2.14.0 (2011-10-31)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RODBC_1.3-3 nnet_7.3-1 doSNOW_1.0.3 foreach_1.3.0
[5] codetools_0.2-8 iterators_1.0.3 snow_0.3-7 randomForest_4.6-2
[9] chron_2.3-42
loaded via a namespace (and not attached):
[1] tools_2.14.0
Run Code Online (Sandbox Code Playgroud)
更新2:
> find("times")
[1] "package:foreach" "package:chron"
> times
function (n)
{
if (!is.numeric(n) || length(n) != 1)
stop("n must be a numeric value")
foreach(icount(n), .combine = "c")
}
<environment: namespace:foreach>
Run Code Online (Sandbox Code Playgroud)
更新3:
> sessionInfo()
R version 2.14.0 (2011-10-31)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] chron_2.3-42
> find("times")
[1] "package:chron"
> 24 * 24 * 60 * (times * (50))
Error in times * (50) : non-numeric argument to binary operator
Run Code Online (Sandbox Code Playgroud)
问题是package:foreach还包含一个名为的函数times.而且因为它出现package:chron在你的搜索路径之前,它"掩盖"了times你真正想要的功能.
换句话说,当R执行其对符号的动态搜索时,times它会在到达与您要查找的函数关联的匹配之前找到匹配(在这种情况下为错误的匹配).
您可以通过启动新的R会话,然后键入以下内容来查看:
> library(chron)
> library(foreach)
Loading required package: iterators
Loading required package: codetools
foreach: simple, scalable parallel programming from Revolution Analytics
Use Revolution R for scalability, fault tolerance and more.
http://www.revolutionanalytics.com
Attaching package: ‘foreach’
The following object(s) are masked from ‘package:chron’:
times
Run Code Online (Sandbox Code Playgroud)
如果你确实需要附加两个软件包,你可以确保通过以下方式获得正确的版本times():颠倒软件包的连接顺序(好但不是很好); 或者(更好)通过键入明确指定您想要的功能chron::times,如:
24 * 24 * 60 * (chron::times(50))
Run Code Online (Sandbox Code Playgroud)