2011-12-31 27 views
2

我試圖將時間轉換爲自午夜以來的秒數。我很難從chron包中獲取times()函數來工作。以下是我如何使用它:R 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" 

有什麼建議嗎?

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 

更新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 
+2

我得到與Josh相同的結果。你可能需要用check.built = TRUE更新R和'update.packages'。至少你需要提供'sessionInfo()' – 2011-12-31 06:05:27

+3

第一個版本,'24 * 24 * 60 *(times(50))',對我來說工作得很好,返回'[1] 1728000'等......你試過'find(「times」)',或者只是輸入'times'來確認R是從''package:chron'''中找到你期望的函數。 – 2011-12-31 06:07:11

回答

6

的問題是,package:foreach還包含一個名爲times功能。並且因爲它在搜索路徑上出現在package:chron之前,所以它掩蓋了您實際需要的times函數。

換句話說,當R對符號times執行動態搜索時,它會在找到與您希望找到的函數關聯的符號之前發現匹配(在這種情況下是錯誤的)。

> 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()正確版本:

您可以通過啓動一個新的R對話,然後鍵入看到以下內容:顛倒包裹的連接順序(OK,但不是很好);或(更好地)通過鍵入chron::times明確指定您想要的功能,如下所示:

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

chron :: times()做到了。非常感謝你! – screechOwl 2011-12-31 06:40:50

+0

Bravo for find(「function-name」)。 – 2011-12-31 06:57:26