将 Excel 文件导入 R、xlsx 或 xls [英] Importing Excel files into R, xlsx or xls

查看:94
本文介绍了将 Excel 文件导入 R、xlsx 或 xls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人帮助我将 excel 2007 (.xlsx) 文件导入 R 的最佳方法.我尝试了几种方法,但似乎都不起作用.我已经升级到2.13.1,windows XP,xlsx 0.3.0,不知道为什么一直出现这个错误.我试过了:

Please can someone help me on the best way to import an excel 2007 (.xlsx) file into R. I have tried several methods and none seems to work. I have upgraded to 2.13.1, windows XP, xlsx 0.3.0, I don't know why the error keeps coming up. I tried:

AB<-read.xlsx("C:/AB_DNA_Tag_Numbers.xlsx","DNA_Tag_Numbers")

AB<-read.xlsx("C:/AB_DNA_Tag_Numbers.xlsx",1)

但我收到错误:

 Error in .jnew("java/io/FileInputStream", file) : 
  java.io.FileNotFoundException: C:\AB_DNA_Tag_Numbers.xlsx (The system cannot find the file specified)

谢谢.

推荐答案

对于没有繁琐的外部依赖项*的解决方案,现在有 readxl:

For a solution that is free of fiddly external dependencies*, there is now readxl:

readxl 包可以轻松地将数据从 Excel 中获取到 R 中.与许多现有软件包(例如 gdata、xlsx、xlsReadWrite) readxl 没有外部依赖,所以很容易在所有操作系统上安装和使用.它旨在与表格数据存储在单个工作表中.

The readxl package makes it easy to get data out of Excel and into R. Compared to many of the existing packages (e.g. gdata, xlsx, xlsReadWrite) readxl has no external dependencies so it's easy to install and use on all operating systems. It is designed to work with tabular data stored in a single sheet.

Readxl 支持旧的 .xls 格式和现代的基于 xml 的格式.xlsx 格式..xls 支持是通过 libxls C 库实现的,它抽象了底层二进制文件的许多复杂性格式.为了解析 .xlsx,我们使用 RapidXML C++ 库.

Readxl supports both the legacy .xls format and the modern xml-based .xlsx format. .xls support is made possible the with libxls C library, which abstracts away many of the complexities of the underlying binary format. To parse .xlsx, we use the RapidXML C++ library.

它可以像这样安装:

install.packages("readxl") # CRAN version

devtools::install_github("hadley/readxl") # development version

用法

library(readxl)

# read_excel reads both xls and xlsx files
read_excel("my-old-spreadsheet.xls")
read_excel("my-new-spreadsheet.xlsx")

# Specify sheet with a number or name
read_excel("my-spreadsheet.xls", sheet = "data")
read_excel("my-spreadsheet.xls", sheet = 2)

# If NAs are represented by something other than blank cells,
# set the na argument
read_excel("my-spreadsheet.xls", na = "NA")

* 并非严格正确,它需要 Rcpp,这反过来又需要 Rtools(适用于 Windows)或 Xcode(适用于 OSX),它们是 R 外部的依赖项.但它们不需要任何路径等操作,因此与 Java 和 Perl 依赖项相比,这是一个优势.

* not strictly true, it requires the Rcpp package, which in turn requires Rtools (for Windows) or Xcode (for OSX), which are dependencies external to R. But they don't require any fiddling with paths, etc., so that's an advantage over Java and Perl dependencies.

更新 现在有 rexcel 包.这有望将 Excel 格式、函数和许多其他类型的信息从 Excel 文件中获取到 R 中.

Update There is now the rexcel package. This promises to get Excel formatting, functions and many other kinds of information from the Excel file and into R.

这篇关于将 Excel 文件导入 R、xlsx 或 xls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆