R chron times() 函数不起作用
我正在尝试将时间转换为自午夜以来的秒数。我很难从 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"
有什么建议吗?
更新:
> 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
更新 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>
更新 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
I'm trying to convert a time to seconds since midnight. I'm having a hard time getting the times() function from the chron package to work. Here's how I'm using it:
> 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"
Any suggestions?
UPDATE:
> 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
UPDATE 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>
UPDATE 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
package:foreach
还包含一个名为times
的函数。由于它出现在搜索路径上的package:chron
之前,因此它“掩盖”了您实际需要的times
函数。换句话说,当 R 对符号
times
执行动态搜索时,它会在找到与您想要的函数关联的匹配项之前找到匹配项(在本例中是错误的匹配项)寻找。您可以通过启动一个新的 R 会话,然后输入以下内容来查看这一点:
如果您确实需要附加两个包,则可以确保获得正确版本的
times()
通过以下任一方法: 颠倒软件包的附加顺序(可以,但不太好);或者(更好)通过输入 chron::times 明确指定您想要的函数,如下所示:The problem is that
package:foreach
also contains a function namedtimes
. And because it appears beforepackage:chron
on your search path, it 'masks' thetimes
function that you actually want.In other words, when R performs its dynamic search for the symbol
times
, it finds a match (the wrong one in this case) before it gets to the one associated with the function you're intending it to find.You can see this by starting a fresh R session, and then typing:
If you do need both packages attached, you can ensure that you get the right version of
times()
by either: reversing the order in which the packages are attached (OK but not great); or (better) explicitly specifying which function you want by typingchron::times
, as in: