7

When I stringr::str_split "\n\na\n" by "\n", I obtain c("", "", "a", "").

I expected I can obtain c("a", "", "", "b") when I stringr::str_split "a\n\nb" by "\n", but I obtained c("a", "", "b") instead. How can I obtain c("a", "", "", "b") by splitting "a\n\n\a"?

Try:

stringr::str_split("a\n\nb", "\n")

Expect:

c("a", "", "", "b")

Result:

c("a", "", "b")
jpsmith
  • 11,023
  • 5
  • 15
  • 36
김용석
  • 71
  • 3

2 Answers2

2

Assuming that we want to extract each character separately, just split on the blank and remove all the \n to return blanks for the split elements

str_remove_all(strsplit(str1, "")[[1]], "\n")
[1] "a" ""  ""  "b"

Or use perl = TRUE with \n?

strsplit(str1, "\n?", perl = TRUE)[[1]]
[1] "a" ""  ""  "b"

If it should be at each \n only

strsplit(str_replace(str1, "(\n\\S)", "\n\\1"), "\n")[[1]]
[1] "a" ""  ""  "b"

data

str1 <- "a\n\nb"
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You are getting the correct result, even if it's not what you expect. The length of the result equals the number of delimiters in the string + 1. Thus if you want an extra field, you need to add an extra delimiter:

x1 <- "a\n\nb"
x2 <- "abc\n\ndef"

strsplit(gsub("(\n{2,})", "\\1\n", x1), "\n")[[1]]
[1] "a" ""  ""  "b"

strsplit(gsub("(\n{2,})", "\\1\n", x2), "\n")[[1]]
[1] "abc" ""    ""    "def"
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56