【问题标题】:Cannot import XLSX file无法导入 XLSX 文件
【发布时间】:2020-03-25 19:51:01
【问题描述】:

我正在尝试将 XLSX 文件导入 R 以开始我的课程项目。它给我带来了问题,因为它返回了一个包含 0 个变量的结果。我试图多次修改文件的路径,但没有不同的结果。有人知道我做错了什么吗?

我的意见:

customer_data <- fread("~C:\\matly\\Desktop\\Grad School\\Class 4\\Customer.xlsx", stringsAsFactors=FALSE)

输出:

The filename, directory name, or volume label syntax is incorrect.
Warning messages:
1: In (if (.Platform$OS.type == "unix") system else shell)(paste0("(",  :
  '(~C:\matly\Desktop\Grad School\Class 4\Customer.xlsx) > C:\Users\matly\AppData\Local\Temp\RtmpGs4FDg\file434f8231' execution failed with error code 1
2: In fread("~C:\\matly\\Desktop\\Grad School\\Class 4\\Customer.xlsx",  :
  File 'C:\Users\matly\AppData\Local\Temp\RtmpGs4FDg\file434f8231' has size 0. Returning a NULL data.table.

【问题讨论】:

  • fread 不读取 XLSX 文件,为此您需要实例包 readxl,函数 read_excel
  • 另外,不确定文件名开头的波浪号 (~) 在做什么...

标签: r import xlsx


【解决方案1】:

这里至少有两个问题:

  • 您的文件名开头有一个假波浪号 (~)
  • data.table::fread() 读取“分隔”文件(即空格或空格或制表符或逗号分隔),而不是 XLSX 文件

试试例如

readxl::read_excel("C:/matly/Desktop/Grad School/Class 4/Customer.xlsx")

其他风格点:

  • read_excel 自动使用stringsAsFactors=FALSE;它返回一个“tibble”,它几乎(但不完全!)与数据框相同
  • 使用/ 作为路径分隔符可以跨平台工作,并且更易于阅读
  • 强烈建议您更改工作目录并使用相对路径名,例如
setwd("C:/matly/Desktop/Grad School/Class 4/")
readxl::read_excel("Customer.xlsx")

【讨论】:

    猜你喜欢
    • 2018-04-13
    • 2016-06-11
    • 1970-01-01
    • 2012-09-17
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2013-10-09
    相关资源
    最近更新 更多