Re: [R] Looking for simple line-splitting code

2025-02-05 Thread Kimmo Elo
Hi, using the library "stringi" allows this: > unlist(stringr::str_split(x, "\n")) [1] "abc" "def" """ghi" Best, Kimmo ke, 2025-02-05 kello 09:35 -0500, Duncan Murdoch kirjoitti: > Thanks to Rui, Peter and Tanvir! Peter's seems to be the fastest of > the > 3 suggestions so far on the littl

Re: [R] Looking for simple line-splitting code

2025-02-05 Thread Duncan Murdoch
On 2025-02-05 5:20 p.m., Rolf Turner wrote: On Wed, 5 Feb 2025 08:44:12 -0500 Duncan Murdoch 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: gh

Re: [R] Looking for simple line-splitting code

2025-02-05 Thread Rui Barradas
Hello, Inline. Às 20:27 de 05/02/2025, peter dalgaard escreveu: A 3rd option could be scan(text=x, what="", blank.lines.skip=FALSE) (all because readLines() doesn't obey the text=x convention, perhaps it should? I'm unsure whether the textConnection is left open in Rui's method.) No, it is

Re: [R] Looking for simple line-splitting code

2025-02-05 Thread Rolf Turner
On Wed, 5 Feb 2025 08:44:12 -0500 Duncan Murdoch 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

Re: [R] Looking for simple line-splitting code

2025-02-05 Thread peter dalgaard
A 3rd option could be scan(text=x, what="", blank.lines.skip=FALSE) (all because readLines() doesn't obey the text=x convention, perhaps it should? I'm unsure whether the textConnection is left open in Rui's method.) -pd > On 5 Feb 2025, at 15:35 , Duncan Murdoch wrote: > > Thanks to Rui, Pe

Re: [R] Looking for simple line-splitting code

2025-02-05 Thread Duncan Murdoch
Thanks to Rui, Peter and Tanvir! Peter's seems to be the fastest of the 3 suggestions so far on the little test case, but on the real data (where x contains several thousand lines), Rui's seems best. Duncan On 2025-02-05 9:13 a.m., peter dalgaard wrote: This also seems to work: strsplit(pa

Re: [R] Looking for simple line-splitting code

2025-02-05 Thread Mohammad Tanvir Ahamed via R-help
x <- c("abc\ndef", "", "ghi") unlist(strsplit(gsub("^$", "\n", x), "\n")) or  x %>%   gsub("^$", "\n", .) %>%   strsplit("\n") %>%   unlist() Regards. Tanvir Ahamed Stockholm, Sweden |  mashra...@yahoo.com On Wednesday, February 5, 2025 at 02:44:37 PM GMT+1, Duncan Murdoch

Re: [R] Looking for simple line-splitting code

2025-02-05 Thread peter dalgaard
This also seems to work: > strsplit(paste(x,collapse="\n"),"\n")[[1]] [1] "abc" "def" """ghi" > On 5 Feb 2025, at 14:44 , Duncan Murdoch 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 >

Re: [R] Looking for simple line-splitting code

2025-02-05 Thread Rui Barradas
Às 13:44 de 05/02/2025, Duncan Murdoch escreveu: 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 e