2017-09-14 48 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来为示例工作定义任意中断。