Reading big data with fixed width

后端 未结 3 1322
[愿得一人]
[愿得一人] 2020-12-02 23:21

How can I read big data formated with fixed width? I read this question and tried some tips, but all answers are for delimited data (as .csv), and that\'s not my case. The d

3条回答
  •  再見小時候
    2020-12-03 00:10

    Here is a pure R solution using the new package readr, created by Hadley Wickham and the RStudio team, released in April 2015. More info here. The code is as simple as this:

    library(readr)
    
    my.data.frame <- read_fwf('TS_MATRICULA_RS.txt',
                          fwf_widths(c(5, 13, 14, 3, 3, 5, 4, 6, 6, 6, 1, 1, 1, 4, 3, 2, 9, 3, 2, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 4, 11, 9, 2, 3, 9, 3, 2, 9, 9, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1)),
                          progress = interactive())
    

    Advantages of read_fwf{readr}

    • readr is based in LaF but surprisingly faster. It has shown to be the fasted method to read fixed-width files in R
    • It's simpler than the alternatives. e.g. you don't need to worry about column_types because they will be imputed from the first 30 rows on the input.
    • It comes with a progress bar ;)

提交回复
热议问题