Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R sqldf - match.fun(asfn) 'c("as.labelled", "as.integer")' is not a function, character or symbol

Tags:

r

sqldf

Total newbie to R, have just spent a couple of hours playing and thought I'd have a play with some of the NHANES datasets e.g. ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2003-2004/

So grabbed a couple and after a play with merge(bmx_c, demo_c) and a quick Google I thought the sqldf library would be a more efficient way to merge / extract just a few columns from the files, to play with, but I've hit a problem.

Error in match.fun(asfn) : 
  'c("as.labelled", "as.integer")' is not a function, character or symbol**

The NHANES files are in SAS format so I had to:

install.packages("Hmisc")
install.packages("sqldf")

library(Hmisc)
library(sqldf)

demo_c <- sasxport.get("DEMO_C.XPT")
bmx_c <- sasxport.get("BMX_C.XPT")

is.data.frame(demo_c)
[1] TRUE

sqldf("select seqn from demo_c")
Error in match.fun(asfn) : 
  'c("as.labelled", "as.integer")' is not a function, character or symbol
>  

summary(demo_c$seqn)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  21000   23540   26070   26070   28600   31130 
> 

I'm guessing some type conversion is required, but I don't know the subtleties of R.

like image 241
arober11 Avatar asked Oct 30 '25 06:10

arober11


1 Answers

sqldf does not support the "labelled" column class produced by Hmisc. All the columns seem to be integer or numeric so convert the columns to numeric first:

demo_c[] <- lapply(demo_c, as.numeric)
sqldf("select seqn from demo_c")

You could convert the integer ones to integer if you prefer:

isInt <- sapply(demo_c, inherits, "integer")
demo_c[isInt] <- lapply(demo_c[isInt], as.integer)    
demo_c[!isInt] <- lapply(demo_c[!isInt], as.numeric)
like image 126
G. Grothendieck Avatar answered Nov 03 '25 00:11

G. Grothendieck