[R] Looking for simple line-splitting code

Rolf Turner ro||turner @end|ng |rom po@teo@net
Wed Feb 5 23:20:45 CET 2025


On Wed, 5 Feb 2025 08:44:12 -0500
Duncan Murdoch <murdoch.duncan using gmail.com> wrote:

> If I have this object:
> 
>    x <- c("abc\ndef", "", "ghi")
> 
> and I write it to a file using `writeLines(x, "test.txt")`, my text 
> editor sees a 5 line file:
> 
>    1: abc
>    2: def
>    3:
>    4: ghi
>    5:
> 
> which is what I'd expect:  the last line in the editor is empty.If I 
> use `readLines("test.txt")` on that file, I get the vector
> 
>    c("abc", "def", "", "ghi")


<SNIP>

Apologies for muddying the waters with my ignorance, but why would you
expect a 5 line file?   I would expect a 4 line file, and that is
indeed what I get when I try the code in question.

If I append an empty line at the end of my test.txt and then
apply readLines() to that file, I get [1] "abc" "def" ""    "ghi" "",
again as *I* would expect.

What am I missing?  Sorry for being a thicko.

cheers,

Rolf

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Stats. Dep't. (secretaries) phone:readLines("test.txt")
         +64-9-373-7599 ext. 89622
Home phone: +64-9-480-4619



More information about the R-help mailing list