我发现了sqldf的一个奇怪的行为 - 当一个变量存在与列和不同数据类型同名时,它会发出“强制引入的NA”警告。以下代码:
x <- structure(list(euring = c(12380, 12430), species = c("Locustella luscinioides",
"Acrocephalus schoenobaenus")), .Names = c("euring", "species"
), row.names = 1:2, class = "data.frame")
species <- structure(list(EURING = c(0, 980), Species = c(NA_integer_, NA_integer_
)), .Names = c("EURING", "Species"), row.names = 1:2, class = "data.frame")
require(sqldf)
result <- sqldf("
select species as Species
from x
")
产生
然后警告信息:在FUN(X [[i]],...):由强制引入的NAs
和result
仅包含NA。
如果变量species
不存在,一切正常。但是sqldf
甚至没有触及变量species
,对吧?
编辑:我怀疑这是一个错误。我报告了它:https://github.com/ggrothendieck/sqldf/issues/23
答案 0 :(得分:1)
很好奇。但您可以通过参考查询中的表来解决它。
> sqldf("select species as Species from x")
Species
1 NA
2 NA
Warning message:
In asfn(rs[[i]]) : NAs introduced by coercion
> sqldf("select a.species as Species from x a")
Species
1 Locustella luscinioides
2 Acrocephalus schoenobaenus
method
为空时有一个错误。 method = data.frame
是另一种选择。
> sqldf("select species as Species from x", method = data.frame)
Species
1 Locustella luscinioides
2 Acrocephalus schoenobaenus