无法导入 XLSX 文件

Cannot import XLSX file

我正在尝试将 XLSX 文件导入 R 以开始我的 class 项目。它给我带来了问题,因为它 returns 具有 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.

这里至少有两个问题:

  • 您的文件名开头有一个伪造的代字号 (~)
  • data.table::fread() 读取 "delimited" 文件(即 space 或白色 space 或制表符或逗号分隔),而不是 XLSX 文件

例如尝试

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

其他风格要点:

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