You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让readxl::read_xls正确读取数据集的第一列?

问题原因
  • 你使用download.file下载xls二进制格式文件时未指定二进制传输模式,是导致第一列读取异常的核心原因。
  • Windows系统下download.file默认采用文本模式传输文件,会损坏xls这类二进制文件的内部结构,读取时就会出现第一列为异常截断字符串的问题。
解决方案

修改下载代码,新增mode = "wb"参数指定二进制下载模式即可:

download.file("http://www.econ.upf.edu/~michael/bioenv.xls", "~/Downloads/bioenv.xls", mode = "wb")
head(readxl::read_xls("~/Downloads/bioenv.xls",
  col_names = c("s", "a", "b", "c", "d", "e", "Pollution", "Depth", "Temperature", "blank", "Sediment"),
  skip = 1))

如果调整后仍存在列类型识别问题,可以手动指定列类型进一步校正:

head(readxl::read_xls("~/Downloads/bioenv.xls",
  col_names = c("s", "a", "b", "c", "d", "e", "Pollution", "Depth", "Temperature", "blank", "Sediment"),
  col_types = c("numeric", rep("numeric", 5), "numeric", "numeric", "numeric", "logical", "text"),
  skip = 1))

内容的提问来源于stack exchange,提问作者jtr13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 20:24:04