R中高效读取文件readxl, read.fwf

来源:互联网 发布:柳叶刀烟瘾 知乎 编辑:程序博客网 时间:2024/05/16 18:58

read.fwf

ff <- tempfile()cat(file = ff, "123456", "987654", sep = "\n")read.fwf(ff, widths = c(1,2,3))    #> 1 23 456 \ 9 87 654
  V1 V2  V31  1 23 4562  9 87 654
read.fwf(ff, widths = c(1,-2,3))   #> 1 456 \ 9 654unlink(ff)
  V1  V21  1 4562  9 654
cat(file = ff, "123", "987654", sep = "\n")read.fwf(ff, widths = c(1,0, 2,3))    #> 1 NA 23 NA \ 9 NA 87 654unlink(ff)cat(file = ff, "123456", "987654", sep = "\n")read.fwf(ff, widths = list(c(1,0, 2,3), c(2,2,2))) #> 1 NA 23 456 98 76 54unlink(ff)

readxl包读取xls,xlsx

读取每个sheets中的数据

excel_sheets(system.file("extdata/datasets.xlsx", package = "readxl"))excel_sheets(system.file("extdata/datasets.xls", package = "readxl"))# To load all sheets in a workbook, use lapplypath <- system.file("extdata/datasets.xls", package = "readxl")lapply(excel_sheets(path), read_excel, path = path)

read_excel

read_excel(path, sheet = 1, col_names = TRUE, col_types = NULL(numeric,date,or text), na = "",skip = 0)
datasets <- system.file("extdata/datasets.xlsx", package = "readxl")read_excel(datasets)# Specific sheet either by position or by nameread_excel(datasets, 2)read_excel(datasets, "mtcars")
0 0
原创粉丝点击