zoukankan      html  css  js  c++  java
  • R 读取excel的方法

    1.加载 readxl 包,利用 reade_excel() 函数

    install.packages("readxl")
    library(readxl)
    data = read_excel("22_data.xlsx",sheet = 1) 

    read_excel函数的参数设置:

    用法:
    read.xlsx(xlsxFile, sheet = 1, startRow = 1, colNames = TRUE,
    rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE,
    skipEmptyCols = TRUE, rows = NULL, cols = NULL, check.names = FALSE,
    namedRegion = NULL, na.strings = "NA", fillMergedCells = FALSE)
    参数:

    startRow :从这一行开始查找数据,无论startRow是多少,文件上面的空行都会被跳过

    sheet : 从那一页开始读

    colNames :如果为真,第一行的数据就是列的名字

    rowNames :如果为真,第一类数据会被作为行的名字

    detectDates :如果为真,则尝试识别日期并进行转换

    skipEmptyRows 如果为真,会跳过空行,如果第一个有数据行之后有空行则返回一行NAs
    If TRUE, empty rows are skipped else empty rows after the first row containing data will return a row of NAs.

    skipEmptyCols 如果为真,会跳过空列
    If TRUE, empty columns are skipped.

    rows 如果为空则读所有的行,否则输入一个向量来读取向量对应的行。

    cols 输入一个数值向量来指定读表格中的那些列,如果为空的化,读完所有的列。

    check.names 逻辑变量,如果为真,则检查数据框中变量的名称,以确保它们是语法上有效的变量名

    namedRegion 工作簿中的命名区域。 如果不是NULL,则 startRow,rows和cols参数将被忽略

    na.strings 字符串的字符向量将会被解释称为 NA,空格将被返回为 NA 。

    fillMergedCells 如果为TRUE,则合并单元格中的值将提供给合并中的所有单元格。

    注意:此函数既可以读 .xls 也可以读.xlsx 类型文件

    2.加载 openxlsx 包,利用 read.xlsx() 函数

    install.packages("openxlsx")
    library(xlsx)
    read.xlsx("22_data.xlsx",sheet=1)

    用法
    read.xlsx(xlsxFile, sheet = 1, startRow = 1, colNames = TRUE,
    rowNames = FALSE, detectDates = FALSE, skipEmptyRows = TRUE,
    skipEmptyCols = TRUE, rows = NULL, cols = NULL, check.names = FALSE,
    namedRegion = NULL, na.strings = "NA", fillMergedCells = FALSE)

    参数设置
    xlsxFile :一个xlsx文件,或者文件的网址

    sheet :从那一页开始读
    The name or index of the sheet to read data from.

    注意:此函数仅可以读取 .xlsx 类型文件

    3.读取文件

    > library(openxlsx)                                 #第一种方式
    > read.xlsx("22_data.xlsx",sheet=1)
          t         y       X1        X2    X3
    1  2000  12581.51 100280.1  15886.50  98.5
    2  2001  15301.38 110863.1  18902.58  99.2
    3  2002  17636.45 121717.4  22053.15  98.7
    4  2003  20017.31 137422.0  24649.95  99.9
    5  2004  24165.38 161840.2  28486.89 102.8
    6  2005  28778.54 187318.9  33930.28 100.8
    7  2006  34804.35 219438.5  40422.73 101.0
    8  2007  45621.97 270232.3  49781.35 103.8
    9  2008  54223.79 319515.5  62592.66 105.9
    10 2009  59521.59 349081.4  76299.93  98.8
    11 2010  73210.79 413030.3  89874.16 103.1
    12 2011  89738.39 489300.6 109247.79 104.9
    13 2012 100614.28 540367.4 125952.97 102.0
    14 2013 110530.70 595244.4 140212.10 101.4
    15 2014 119175.31 643974.0 151785.56 101.0
    16 2015 124922.20 689052.1 175877.77 100.1
    17 2016 130360.73 743585.5 187755.21 100.7
    > library(readxl) #第二种方式 > data = read_excel("22_data.xlsx",sheet = 1) > data # A tibble: 17 x 5 t y X1 X2 X3 <dbl> <dbl> <dbl> <dbl> <dbl> 1 2000 12582. 100280. 15886. 98.5 2 2001 15301. 110863. 18903. 99.2 3 2002 17636. 121717. 22053. 98.7 4 2003 20017. 137422 24650. 99.9 5 2004 24165. 161840. 28487. 103. 6 2005 28779. 187319. 33930. 101. 7 2006 34804. 219438. 40423. 101 8 2007 45622. 270232. 49781. 104. 9 2008 54224. 319516. 62593. 106. 10 2009 59522. 349081. 76300. 98.8 11 2010 73211. 413030. 89874. 103. 12 2011 89738. 489301. 109248. 105. 13 2012 100614. 540367. 125953. 102 14 2013 110531. 595244. 140212. 101. 15 2014 119175. 643974 151786. 101 16 2015 124922. 689052. 175878. 100. 17 2016 130361. 743586. 187755. 101.

     注意:对于同样的数据,两个函数的输出结果并不一样,当然理想的方式是上程序框的第一个函数。

  • 相关阅读:
    SecureCRT乱码问题简单的解决办法
    安家博客园,开始java web 之旅
    Java Service Wrapper使用心得
    vsftp折腾
    mysql更改数据库表名称和svnerve启动版本库命令、执行jar命令
    Linux 命令积累2
    二进制与十进制的转换
    java Map代替List在for循环中的应用
    Optional 的应用
    java Date、java.sql.Date、localTime、SimpleDateFormat的格式及应用
  • 原文地址:https://www.cnblogs.com/jiaxinwei/p/11520923.html
Copyright © 2011-2022 走看看