R chron times() 函数不起作用

发布于 2024-12-23 22:41:26 字数 1999 浏览 1 评论 0原文

我正在尝试将时间转换为自午夜以来的秒数。我很难从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2024-12-30 22:41:26

问题是 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

如果您确实需要附加两个包,则可以确保获得正确版本的times()通过以下任一方法: 颠倒软件包的附加顺序(可以,但不太好);或者(更好)通过输入 chron::times 明确指定您想要的函数,如下所示:

24 * 24 * 60 * (chron::times(50))

The problem is that package:foreach also contains a function named times. And because it appears before package:chron on your search path, it 'masks' the times 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:

> 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

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 typing chron::times, as in:

24 * 24 * 60 * (chron::times(50))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文