R语言在读取Exel数据上表现不太好,虽然也有一些不错的包,但是体验很差,所以我个人一般都是把Exel转化成csv或者txt来读取,而现在可以在CRAN上使用readxl 1.2.0。 在http://readxl.tidyverse.org上了解有关readxl的更多信息。
readxl软件包可以很容易地从Excel文件中获取表格数据,并使用代码而不是鼠标点击来获取R。 它支持.xls格式和基于XML的.xlsx格式。 readxl易于在所有操作系统上安装和使用。 因此,它没有外部依赖,例如Java或Perl,其中一些R包读取Excel文件。
# install.packages("readxl")
library(readxl)
setwd('D:/R.Rstudio/R/R-3.5.1/library/readxl/extdata')
xls_eg <- read_excel("datasets.xls")
xls_eg
xls_iris <- read_excel("datasets.xls", sheet =1)
xls_iris
xls_iris <- read_excel("datasets.xls", sheet =1,range = cell_cols("B:D"))
xls_iris
xls_iris <- read_excel("datasets.xls", sheet =1,n_max = 5)
xls_iris
xls_iris <- read_excel("datasets.xls", sheet =1,range = "A1:C5")
xls_iris
xls_iris <- read_excel("datasets.xls", sheet =1,col_names = FALSE,n_max = 5)
xls_iris
xls_iris <- read_excel("datasets.xls", sheet =1,
n_max = 5,col_names = c("x1","x2","x3","x4","y"))
xls_iris
xls_iris <- read_excel("datasets.xls", sheet =1,
n_max = 5,col_types = c("numeric","numeric","numeric","numeric","text"))
xls_iris
## pass custom function to implement "lower_snake_case"
my_custom_name_repair <- function(nms) tolower(gsub("[.]", "_", nms))
read_excel(
readxl_example("datasets.xlsx"), n_max = 3,
.name_repair = my_custom_name_repair
)
read_excel(
readxl_example("datasets.xlsx"), sheet = "chickwts", n_max = 3,
.name_repair = ~ substr(.x, start = 1, stop = 3)
)
# A tibble: 3 x 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <chr>
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
# A tibble: 3 x 2
weight feed
<dbl> <chr>
1 179 horsebean
2 160 horsebean
3 136 horsebean