如何将变量未存储在同一行中并且缺少从列到列的标准分隔符的文本文件读入 R?

问题描述

我正在尝试将文本文件 (https://www.bls.gov/bdm/us_age_naics_00_table5.txt) 读入 R,但我不确定如何解析它。如您所见,列名(年份)并非都位于同一行上,并且列与列之间的数据之间的空间不一致。我熟悉使用 read.csv()read.delim(),但我不知道如何阅读这样一个复杂的文件

解决方法

这是一个手动解析:

    for(int i=index; i<size;i++){
        l.push_back(arr[i]); // THIS IS FLIPPING BIT AT INDEX I to a 1
        // THE BELOW GENERATES ALL POSSIBLE BINARY NUMBERS SO THAT WHEN ANY ONE IS APPENDED TO OUR CURRENT BINARY NUMBER IT'S LENGTH IS N
        subset(arr,size,choices_left-1,i+1,l); 
        l.pop_back(); // FLIPPING THE BIT AT INDEX I BACK TO 0
    }

如果您想将“_”重新编码为 require(readr) string = read_lines(file="https://www.bls.gov/bdm/us_age_naics_00_table5.txt") string = string[nchar(string) != 0] string = string[-c(1,2)] # don't contain information string = string[string != " "] string = string[-151] # footnote sMatrix = matrix(string,nrow = 30) dfList = sapply(1:ncol(sMatrix),function(x) readr::read_table(paste(sMatrix[,x]))) df = do.call(cbind,dfList) df = df[,!duplicated(colnames(df))] # removes columns with duplicate names ,并格式化数字:

NA