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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With