为什么 data.table 不知道 "J"?
Why does data.table not know "J"?
我 运行 RStudio 在调试模式下检查包中的某些功能(它使用 data.table 并且已经工作了 2 年)。现在我在某些功能中遇到以下情况:
routes[J(x1, y1, x2, y2), nomatch = 0L]
Error in J(x1, y1, x2, y2) : could not find function "J"
如前所述:此包有效并且 library(data.table)
已设置 .
现在我想,我按照建议做了一个小检查here - 环境不变:
dt = data.table( id = 1L, start = c( 9, 21, 5 ), end = c( 10, 22, 7 ) )
data.table::setkey(dt, "start")
dt[J(1), nomatch = 0L]
Empty data.table (0 rows) of 3 cols: id,start,end
我得到了正确的结果?
如果有人有想法,如何创建可重现的示例,我会尝试。这对我来说没有任何意义...所以我再次尝试,但是
routes[J(x1, y1, x2, y2), nomatch = 0L]
Error in J(x1, y1, x2, y2) : could not find function "J"
非常感谢任何提示
截图:
可重现示例 data.table 中有多个参数:
library(data.table)
r <- data.table::data.table(lat1 = numeric(0), lng1 = numeric(0),
lat2 = numeric(0), lng2 = numeric(0),
time = numeric(0))
data.table::setkey(r, lat1, lng1, lat2, lng2)
lat1 = 1
lat2 = 2
lng1 = 11
lng2 = 22
li <- data.table::data.table(lat1=lat1, lng1=lng1, lat2=lat2, lng2=lng2, time=time)
r <- rbindlist(list(r, li))
data.table::setkey(r, lat1, lng1, lat2, lng2)
r[J(1, 11, 2, 22), nomatch = 0L]
# lat1 lng1 lat2 lng2 time
# 1: 1 11 2 22 <function>
r[J(1, 11, 2, 0), nomatch = 0L]
# Empty data.table (0 rows) of 5 cols: lat1,lng1,lat2,lng2,time
But still:
routes[J(1, 11, 2, 22), nomatch = 0L]
# Error in J(1, 11, 2, 22) : could not find function "J"
J
不是函数。它是在评估之前被替换的别名:
https://github.com/Rdatatable/data.table/blob/master/R/data.table.R#L102
我怀疑 routes
不是 data.table。
在这里添加我的评论,因为它们说明了我的思考过程,让我找到了解决方案:
我回答了一个非常相似的问题,有人在寻找 .
函数。所以,我对此非常确定,只需要搜索 J
的源代码。一旦我确认这是一个像 .
这样的别名,就很容易推断出 [.data.table
不可能被您的代码调用。
您调用了 [
,它有(超过)2 个参数。所以,那里没有错误。只有第一个参数求值时,才会报错。
我 运行 RStudio 在调试模式下检查包中的某些功能(它使用 data.table 并且已经工作了 2 年)。现在我在某些功能中遇到以下情况:
routes[J(x1, y1, x2, y2), nomatch = 0L]
Error in J(x1, y1, x2, y2) : could not find function "J"
如前所述:此包有效并且 library(data.table)
已设置 .
现在我想,我按照建议做了一个小检查here - 环境不变:
dt = data.table( id = 1L, start = c( 9, 21, 5 ), end = c( 10, 22, 7 ) )
data.table::setkey(dt, "start")
dt[J(1), nomatch = 0L]
Empty data.table (0 rows) of 3 cols: id,start,end
我得到了正确的结果? 如果有人有想法,如何创建可重现的示例,我会尝试。这对我来说没有任何意义...所以我再次尝试,但是
routes[J(x1, y1, x2, y2), nomatch = 0L]
Error in J(x1, y1, x2, y2) : could not find function "J"
非常感谢任何提示
截图:
可重现示例 data.table 中有多个参数:
library(data.table)
r <- data.table::data.table(lat1 = numeric(0), lng1 = numeric(0),
lat2 = numeric(0), lng2 = numeric(0),
time = numeric(0))
data.table::setkey(r, lat1, lng1, lat2, lng2)
lat1 = 1
lat2 = 2
lng1 = 11
lng2 = 22
li <- data.table::data.table(lat1=lat1, lng1=lng1, lat2=lat2, lng2=lng2, time=time)
r <- rbindlist(list(r, li))
data.table::setkey(r, lat1, lng1, lat2, lng2)
r[J(1, 11, 2, 22), nomatch = 0L]
# lat1 lng1 lat2 lng2 time
# 1: 1 11 2 22 <function>
r[J(1, 11, 2, 0), nomatch = 0L]
# Empty data.table (0 rows) of 5 cols: lat1,lng1,lat2,lng2,time
But still:
routes[J(1, 11, 2, 22), nomatch = 0L]
# Error in J(1, 11, 2, 22) : could not find function "J"
J
不是函数。它是在评估之前被替换的别名:
https://github.com/Rdatatable/data.table/blob/master/R/data.table.R#L102
我怀疑 routes
不是 data.table。
在这里添加我的评论,因为它们说明了我的思考过程,让我找到了解决方案:
我回答了一个非常相似的问题,有人在寻找 .
函数。所以,我对此非常确定,只需要搜索 J
的源代码。一旦我确认这是一个像 .
这样的别名,就很容易推断出 [.data.table
不可能被您的代码调用。
您调用了 [
,它有(超过)2 个参数。所以,那里没有错误。只有第一个参数求值时,才会报错。