Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (50.2k points)

I want to read a text file that appears to have above-tabulated data that I need to analyze in R. The number of lines of unnecessary text is never the same between two files. But, I know that there is always one indistinguishable string of text at the end of the irrelevant content and before the start of the tabulated data (i.e., "ID Only Records:").

I basically want to extract all of the data in the columns named "Date" "Time" "Channel" "Tag ID" "Antenna" and "Power"

enter image description here

1 Answer

0 votes
by (108k points)

I think you are not knowing how many lines you need to skip. In that case, you can use the readLines() function to read all the text from the text file. After reading just find the line number using grep() the text 'ID only records:' and start gathering the data from that line by collapsing the text into one string. Refer to the below command:

temp <- readLines('temp.txt')

output <- read.table(text = paste0(trimws(temp[(

                 grep('ID only records :', temp) + 1) : length(temp)]), 

                collapse = "\n"), header = TRUE)

If you are a beginner and want to know more about R then do check out the R programming course, that will help you learning R in a better way.  

Browse Categories

...