Skip to content

readLines vs scan

2 messages · Bert Gunter, Gabor Grothendieck

#
Folks:

Suppose I wish to input a text file with variable length lines and
possible whitespace as is and then parse the resulting character
vector in R. Each line of text is terminated with "\n" (newline
character).

Is there any reason to prefer one or the other of:

scan (filename, what ="a",sep ="\n")  ##or
readLines(filename)

If it makes a difference, I'm on Windows.

Many thanks for any advice/insight.
#
On Sun, Feb 12, 2012 at 10:35 AM, Bert Gunter <gunter.berton at gene.com> wrote:
It depends on whether we need to retain the information regarding
which elements were on the same line or not.  In the first case we
retain that info and in the second case we lose it:
Read 2 items
Read 3 items
[[1]]
[1] 1 2

[[2]]
[1] 3 4 5
Read 5 items
[1] 1 2 3 4 5

If we did want to get back the info we lost in the last instance we
need to re-read it:
$`1`
[1] 1 2

$`2`
[1] 3 4 5