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

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

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

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

这是我的数据的样子。我在一栏中有横断面,而另一栏中有横断面。在右侧,您可以看到distance是一个num值,而横断面是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"))

最佳答案

dput输出显示出您正在提供的是tbl_df(一个tidyverse标题),而不是标准的data.frameformatDistData似乎在小玩意上窒息了。如果您这样做:

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定义了任意中断,以使示例正常工作。

关于r - formatDistData(),距离必须为数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46228611/

10-12 05:55