2017-09-14 46 views
0

此問題可能已被重複。但即使經歷了前面的鏈接,我也無法解決這個問題。formatDistData(),距離必須爲數字

我試圖使用formatDistData()函數從無人盯防的包裝,但是,運行代碼

yDat <- formatDistData(Detects1, distCol="distance", transectNameCol="transect", dist.breaks=db) 

,當我得到一個錯誤說"The distances must be numeric"。我已經運行了as.numeric()函數,但代碼顯示相同的錯誤。

以下是我的數據外觀。我在一列和另一列的距離橫斷。在右邊你可以看到距離是一個數值,橫斷面是chr。如果有人能告訴我什麼,我需要爲了做它的工作,將是巨大

Data

transect,distance 
NT1,14 
NT1,14 
NT1,20 
NT1,20 
NT1,10 
NT1,15 






> dput(Detects1)` 
structure(list(transect = c("NT1", "NT1", "NT1", "NT1", "NT1", 
"NT1", "NT1", "NT1", "NT1", "NT1", "NT2", "NT2", "NT2", "NT2", 
"NT2", "NT2", "NT2", "NT2", "NT2", "NT2", "NT2", "NT2", "NT3", 
"NT3", "NT3", "NT3", "NT3", "NT3", "NT3", "NT3", "NT3", "NT3", 
"NT3", "NT3", "NT3", "NT3", "NT3", "NT3", "NT4", "NT3", "NT4", 
"NT4", "NT4", "NT5", "NT5", "NT5", "NT5", "NT5", "SCC1", "SCC1", 
"SCC1", "SCC1", "SCC1", "SCC1", "SCC1", "SCC1", "SCC3", "SCC3", 
"SCC4", "SCC4", "SCC4", "SCC4", "SCC4", "SCC5", "SCC5", "SCC5", 
"SCC5", "SCC5", "SCC5", "SCC5", "Urban1", "Urban1", "Urban1", 
"Urban2", "Urban2", "Urban2", "Urban2", "Urban2", "Urban2", "Urban2", 
"Urban4", "Urban4"), distance = c(14, 14, 20, 20, 10, 15, 5, 
10, 15, 6, 10, 5, 5, 40, 7, 7, 5, 5, 12, 12, 2, 2, 5, 16, 6, 
13, 3, 7, 5, 2, 0, 16, 10, 20, 20, 15, 10, 11, 17, 17, 12, 5, 
3, 5, 8, 21, 12, 12, 15, 15, 7, 12, 3, 5, 6, 3, 2, 7, 8, 21, 
5, 5, 11, 4, 12, 2, 1, 2, 5, 14, 10, 8, 3, 3, 11, 4, 9, 3, 7, 
5, 2, 7)), .Names = c("transect", "distance"), row.names = c(NA, 
-82L), spec = structure(list(cols = structure(list(transect = 
structure(list(), class = c("collector_character", 
"collector")), distance = structure(list(), class = c("collector_number", 
"collector"))), .Names = c("transect", "distance")), default = 
structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"), class = 
c("tbl_df", "tbl", "data.frame")) 
+0

請以易於粘貼的形式提供您的數據的一個小例子。 –

+0

它會幫助,如果你讓你的問題可重現:https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

+0

@RomanLuštrik這樣好嗎? – Jamie

回答

0

dput輸出顯示我說,你所提供的是tbl_df(一tidyverse tibble)而不是標準data.frame。看來formatDistData t on t。。如果你這樣做:

library(unmarked) 
yDat <- formatDistData(
    as.data.frame(Detects1), # coerce to standard data.frame 
    distCol="distance", 
    transectNameCol="transect", 
    dist.breaks = quantile(Detects1$distance, seq(0.1, 0.9, by = 0.1))) 

它會工作。

請注意,您沒有提供db,因此我使用quantile來爲示例工作定義任意中斷。