rrowread.table

Reading .txt files with no separators between Columns (column's names) and Rows (column's content)


I have several .txt files that I would like to read and then rbind in R. I expect that each .txt file generate 1 line and 115 columns. First problem: I’m facing the following Warning message: “incomplete final line found by readTableHeader on…” But I have several files and I can’t navigate to the last line of each file and press Enter. Some solutions I found in the Internet didn’t work because of the following second problem.

Second problem: the column names (Columns) and the content of the columns (Rows) have no separator. The .txt files looks like this: "DIARREIA":1,"DISPNEIA":2, note that "DIARREIA" and "DISPNEIA" are column names while 1 and 2 are column contents. There are colon (:) between the name of the column and the content of the column.

Here is my code and 2 files as examples are available at https://drive.google.com/drive/folders/16U8J12Ld7PI5DI-ph_2QCysTxFGKZ-QP?usp=share_link.

````setwd("C:/User/BOX")
    unzip("C:/User/BOX/data.zip")
    list.files()
    temp = list.files(pattern = "*.txt")
    df = do.call("rbind", lapply(temp, function(x) read.table(x, stringsAsFactors = T, header = TRUE)))```

Any help, please? Thanks in advance!


Solution

  • Hello Baptista: install jsonlite if you dont installed it and try this:

    # this line installs jsonlite
    if(!("jsonlite" %in% installed.packages())) install.packages("jsonlite")
    
    setwd("C:/User/BOX")
    unzip("C:/User/BOX/data.zip")
    temp <- list.files(pattern = "*.txt")
    df <- do.call("rbind", lapply(temp, jsonlite::read_json))