R chron times()函数不起作用

时间:2011-12-31 05:49:06

标签: r chron

我试图将时间从午夜转换为秒。我很难从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

1 个答案:

答案 0 :(得分:6)

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