You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
rec_with_table() fails with 'list' object cannot be coerced to type 'double' when input data contains haven_labelled columns from SPSS/Stata imports via the haven package.
This issue emerged when using PUMF data from ODESSI, which generates data using haven. Previous testing used CSV-sourced data which doesn't carry the haven_labelled class.
Reproducible example
library(haven)
library(cchsflow)
# Load SPSS data (creates haven_labelled columns)pumf<-haven::read_sav("cchs_2015-2016_pumf.sav")
# Check column class
class(pumf$HWTGHTM)
# [1] "haven_labelled" "vctrs_vctr" "double"# This failsresult<- rec_with_table(pumf, variables="HWTGHTM", database_name="cchs2015_2016_p")
# Error in recode_call[x] : 'list' object cannot be coerced to type 'double'# Compare with package test data (plain numeric) - works fine
data("cchs2001_p", package="cchsflow")
class(cchs2001_p$HWTGHTM)
# [1] "numeric"result<- rec_with_table(cchs2001_p, variables="HWTGHTM", database_name="cchs2001_p")
# Success
Root cause
haven_labelled columns carry SPSS metadata (value labels, missing codes) as attributes. When rec_with_table() performs numeric operations on these columns, R's type coercion fails.
The underlying values are correct - it's the class wrapper that causes issues:
> as.numeric(pumf$HWTGHTM[1:5])
[1] 1.731.651.80NA1.58# Values are fine
Current workaround
Convert haven_labelled columns to plain numeric before calling rec_with_table():
# Option 1: Manual conversiondf<- as.data.frame(df)
df[] <- lapply(df, function(x) {
if (inherits(x, "haven_labelled")) as.numeric(x) elsex
})
# Option 2: Use haven::zap_labels()df<-haven::zap_labels(df)
# Now rec_with_table worksresult<- rec_with_table(df, variables="HWTGHTM", database_name="cchs2015_2016_p")
Proposed fix
Add automatic detection and conversion of haven_labelled columns in rec_with_table():
# At start of rec_with_table()if (any(sapply(data, inherits, "haven_labelled"))) {
message("Converting haven_labelled columns to numeric")
data<- as.data.frame(data)
data[] <- lapply(data, function(x) {
if (inherits(x, "haven_labelled")) as.numeric(x) elsex
})
}
Problem
rec_with_table()fails with'list' object cannot be coerced to type 'double'when input data containshaven_labelledcolumns from SPSS/Stata imports via thehavenpackage.This issue emerged when using PUMF data from ODESSI, which generates data using
haven. Previous testing used CSV-sourced data which doesn't carry thehaven_labelledclass.Reproducible example
Root cause
haven_labelledcolumns carry SPSS metadata (value labels, missing codes) as attributes. Whenrec_with_table()performs numeric operations on these columns, R's type coercion fails.The underlying values are correct - it's the class wrapper that causes issues:
Current workaround
Convert
haven_labelledcolumns to plain numeric before callingrec_with_table():Proposed fix
Add automatic detection and conversion of
haven_labelledcolumns inrec_with_table():Context
havenbecomes the standard for importing SPSS/Stata data in R, this issue will affect more usershaven-generated filesceps/cep-003-physical-activity/haven_labelled_issue.md