使用skimr处理REDCap导出数据时出现vctrs属性不兼容错误
问题描述
使用skimr包的skim()函数探索REDCap导出的数据时,触发以下错误:
Error in `dplyr::summarize()`: ! Problem while computing `skimmed = purrr::map2(...)`. ℹ The error occurred in group 4: skim_type = "numeric". Caused by error: ! Can't combine `..1$numeric.p0` <labelled> and `..2$numeric.p0` <labelled>. ✖ Some attributes are incompatible. ℹ The author of the class should implement vctrs methods. ℹ See <https://vctrs.r-lib.org/reference/faq-error-incompatible-attributes.html>. Backtrace: 1. skimr::skim(e2) 10. purrr::map2(...) 12. skimr:::skim_by_type.data.frame(.x[[1L]], .y[[1L]], ...) 13. skimr:::build_results(skimmed, variable_names, NULL) 15. tidyr:::unnest.data.frame(out, .data$by_variable) ... 19. vctrs (local) `<fn>`() 20. vctrs::vec_default_ptype2(...) 21. vctrs::stop_incompatible_type(...) 22. vctrs:::stop_incompatible(...) 23. vctrs:::stop_vctrs(...) Error in dplyr::summarize(grouped, skimmed = purrr::map2(.data$skimmers, : ℹ The error occurred in group 4: skim_type = "numeric". Caused by error: ! Can't combine `..1$numeric.p0` <labelled> and `..2$numeric.p0` <labelled>. ✖ Some attributes are incompatible. ℹ The author of the class should implement vctrs methods. ℹ See <https://vctrs.r-lib.org/reference/faq-error-incompatible-attributes.html>.
错误原因
REDCap导出的数据会给变量附加labelled类型属性(如变量标签、编码说明),skimr汇总时,vctrs包无法合并属性不兼容的labelled对象,导致报错。
解决方案
移除所有变量标签:用
haven::zap_labels()移除标签属性,转换为基础数据类型后再执行skim:library(haven) library(skimr) # 移除数据中所有变量的标签 data_cleaned <- zap_labels(your_redcap_data) # 执行探索性分析 skim(data_cleaned)仅转换数值型变量:针对报错的数值型变量单独转换类型,保留其他变量的标签:
library(dplyr) library(skimr) your_redcap_data <- your_redcap_data %>% mutate(across(where(is.numeric), as.numeric)) skim(your_redcap_data)更新依赖包:确保skimr、dplyr、vctrs和haven为最新版本,新版本可能修复了兼容性问题:
update.packages(c("skimr", "dplyr", "vctrs", "haven"))
内容的提问来源于stack exchange,提问作者Yue Pan
相关产品推荐
相关产品推荐

