Reading text file that is "next line delimited"

I'm not sure how to phrase this question correctly, so I'll give an example.
Let say I have a .txt file where the first row is a DATE and the second is a TEXT. Then the pattern repeats, 3rd rows is DATE, 4th is TEXT. How would I read this into R and convert it into two column dataframe.
Essentially I see it as being "next line delimited", not sure if this is a correct way to describe it.

Here is a sample data that would be saved in a .txt file:

.LOG
3:42 PM 12/04/2019
Task 1
3:45 PM 12/04/2019
Task 2
3:55 PM 13/04/2019
Task 3
3:47 PM 15/04/2019

I want it to look like:

DATE                TEXT
3:42 PM 12/04/2019  Task 1
3:45 PM 12/04/2019  Task 2
3:55 PM 13/04/2019  Task 3

3 answers

  • answered 2019-04-15 06:03 Ronak Shah

    Read the file using read.table with sep = "\n" so you'll have single column dataframe

    df <- read.table(text = "3:42 PM 12/04/2019
                     Task 1
                     3:45 PM 12/04/2019
                     Task 2
                     3:55 PM 13/04/2019
                     Task 3", sep = "\n")
    

    For reading it from a file do

    df <- read.table("path_of_the_file.txt", sep = "\n")
    

    Now split it into two columns by selecting alternate rows

    data.frame(Date = df[c(TRUE, FALSE), ], Text = df[c(FALSE, TRUE), ])
    
    #               Date    Text
    #1 3:42 PM 12/04/2019 Task 1
    #2 3:45 PM 12/04/2019 Task 2
    #3 3:55 PM 13/04/2019 Task 3
    

  • answered 2019-04-15 06:06 LauraS

    You could do something like this:

     MessyData <- read.table("Myfile.txt")
     CleanData <- data.frame(DATE = MessyData[seq(1, length(MessyData), by = 2)],
                             TEXT = MessyData[seq(2, length(MessyData), by = 2)])
    

  • answered 2019-04-15 06:15 akrun

    Here is much better option with matrix method

    as.data.frame(matrix(trimws(df$V1), ncol = 2, byrow = TRUE, 
              dimnames = list(NULL, c("DATE", "TEXT"))))
    #               DATE   TEXT
    #1 3:42 PM 12/04/2019 Task 1
    #2 3:45 PM 12/04/2019 Task 2
    #3 3:55 PM 13/04/2019 Task 3
    

    after reading the dataset

    data

    df <- read.table("file.txt", sep = "\n")