abc
abc

Reputation: 177

How to make R stop reading rows in a text file at a line containing a specific character?

For example, I want to read lines from the beginning of a text file up to a string with ";" symbol excluding this string.

Thanks a lot.

Upvotes: 2

Views: 825

Answers (1)

Paul Hiemstra
Paul Hiemstra

Reputation: 60984

A very simple approach might be to read the contents of the using readLines:

content = readLines("data.txt")

And then split the character data on the ;:

split_content = strsplit(content, split = ";")

And then extract the first elememt, i.e. the text up to the semicolon:

first_element = lapply(split_content, "[[", 1]

The result is a list of all the text in the rows of the data file up to the semicolon.

Ps I'm not entirely sure about the last line...I can't check it as I've got no access to R right now.

Upvotes: 1

Related Questions