R chron times()函数将不起作用 [英] R chron times() function won't work

查看:111
本文介绍了R chron times()函数将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将时间从午夜开始转换为秒.我很难从chron包中获取times()函数来工作.这是我的使用方式:

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"

有什么建议吗?

更新:

> 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

推荐答案

问题是package:foreach还包含一个名为times的函数.而且由于它出现在搜索路径上的package:chron之前,因此它掩盖"了您实际想要的times函数.

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.

换句话说,当R对符号times进行动态搜索时,它会在找到与要查找的功能关联的匹配项之前找到匹配项(在这种情况下为错误的匹配项).

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.

您可以通过开始一个新的R会话,然后键入:

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

如果您需要同时附加两个软件包,则可以通过以下两种方法之一来确保获得正确版本的times():颠倒软件包的附加顺序(可以,但不是很好);或(更好)通过键入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))

这篇关于R chron times()函数将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆