Tomas
Tomas

Reputation: 59525

Strange warning message: In FUN(X[[i]], ...) : NAs introduced by coercion

I found a strange behaviour of sqldf - it issues "NAs introduced by coercion" warning when a variable exists with the same name as a column and different data type. The following code:

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
")

produces:

Warning message: In FUN(X[[i]], ...) : NAs introduced by coercion

and result then contains only NAs.

If variable species didn't exist, everything is OK. But sqldf shoudln't even touch the variable species, right??

EDIT: I suspect it is a bug. I reported it: https://github.com/ggrothendieck/sqldf/issues/23

Upvotes: 2

Views: 1988

Answers (1)

It's curious. But you can solve it by referring to the table in the query.

> 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

There is a bug when method is null. method = data.frame is another option.

> sqldf("select species as Species from x", method = data.frame)
                     Species
1    Locustella luscinioides
2 Acrocephalus schoenobaenus

Upvotes: 2

Related Questions