R语言中如何读取excel文件

一、readxl读取

1、准备测试数据

 

 

 

 

2、安装、加载readxl包

install.packages("readxl")
library(readxl)

 

3、测试

> test <- read_xlsx("test.xlsx",1)
> test
# A tibble: 2 x 2
      a     b
  <dbl> <dbl>
1   100   200
2   500   800
> class(test)
[1] "tbl_df" "tbl" "data.frame"
> dim(test)
[1] 2 2

> test2 <- read_xlsx("test.xlsx",2)
> test2
# A tibble: 3 x 3
      a     b     c
  <dbl> <dbl> <dbl>
1   400   200   100
2   800   900   300
3   700   600   500
> class(test2)
[1] "tbl_df" "tbl" "data.frame"
> dim(test2)
[1] 3 3

 

二、xlsx读取

> library(xlsx)   ## 提前安装好这些包
> library(xlsxjars)
> library(rJava)
> a <- read.xlsx("test.xlsx",1,header = F)
> a
   X1  X2
1   a   b
2 100 200
3 500 800
> dim(a)
[1] 3 2
> class(a)
[1] "data.frame"
> b <- read.xlsx("test.xlsx",2,header = F)
> b
   X1  X2  X3
1   a   b   c
2 400 200 100
3 800 900 300
4 700 600 500
> dim(b)
[1] 4 3
> class(b)
[1] "data.frame"

 

posted @ 2020-12-20 20:19  小鲨鱼2018  阅读(10273)  评论(0编辑  收藏  举报