Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 606
jmich738 Avatar asked Dec 22 '22 23:12

jmich738


1 Answers

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
like image 198
Ronak Shah Avatar answered Jan 04 '23 15:01

Ronak Shah