formatDistData(),距离必须为数字

时间:2017-09-14 21:37:56

标签: r error-handling distance unmarked-package

这个问题可能已经重复过了。但即使经过前面的链接,我也无法解决这个问题。

我正在尝试使用未标记包中的formatDistData()函数,但是,在运行代码时

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

我收到错误"The distances must be numeric"。我已经运行了as.numeric()函数,但代码显示相同的错误。

以下是我的数据的外观。我在一列中横断并在另一列中有距离。在右侧,您可以看到距离是num值,transect是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"))

1 个答案:

答案 0 :(得分:0)

dput输出显示您提供tbl_dftidyverse tibble)而不是标准data.frame。似乎formatDistData在chibbles上窒息。如果你这样做:

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为示例定义任意中断。