R|批量读取

作者: 高大石头 | 来源:发表于2021-04-12 00:08 被阅读0次

    批量读取excel表单

    日常工作学习中,一个excel中往往有多个表单,逐个读取会非常麻烦,这个时候就可以发挥R批量操作的功效啦。
    readxl核心函数:

    • read_excel():读取excel文件
    • excel_sheets():列出一个excel文件下的所有sheets
    library(readxl)
    path <- "example1.xlsx"
    
    #方法一
    df1 <- lapply(excel_sheet(path), function(x) read_excel(path,sheet = x))
    
    #方法二
    df2 <- excel_sheets(path) %>% 
      set_names() %>% 
      map(read_excel, path=path)
    

    番外:

    熟悉tidyverse的朋友对readr应该不会陌生,其实readr和readxl比较类似,它们都会主动的去猜测数据的类型,只不过猜测的方式不太一样。

    • readr根据数据猜测列的类型
    • readxl根据Excel单元格类型,猜测列类型
      Excel表格单元格类型的优先级:
      empty<boolean<numeric<text

    批量读取csv文件

    f <- list.files("./rawdata", pattern = "*.csv", full.names = TRUE)
    d <- purrr::map_df(f, readr::read_csv,.id="id")
    

    参考链接:
    readxl:Read Excel Files
    批量读取文件

    相关文章

      网友评论

        本文标题:R|批量读取

        本文链接:https://www.haomeiwen.com/subject/fkqtlltx.html