Giter VIP home page Giter VIP logo

Comments (6)

hadley avatar hadley commented on May 29, 2024

The behaviour of strsplit confused me a bit. Is this consistent?

strsplit("", "")
# [[1]]
character(0)
strsplit(" ", " ")
# [[1]]
# [1] ""

from stringr.

kohske avatar kohske commented on May 29, 2024

After reading the algorithm in ?strsplit, I found this is consistent but not typical. Usually (i.e., in the other languages), the second command should return 2 empty characters such as ["", ""].

from stringr.

joranE avatar joranE commented on May 29, 2024

The latter seems correct to me, since it should behave the same as

> strsplit("a","a")
[[1]]
[1] ""

But I think once you've decided that splitting on "" means to 'split into individual characters', whether strsplit("","") should return "" or character(0) seems like a judgement call to me.

My issue with the behavior of str_split is that pulling only the leading zero-length string seems very arbitrary. One could argue that there are infinitely many instances of "" in every string! Or at least one between every single character. So why shouldn't it return something like:

str_split("abc","")
[[1]]
[1] "" "a" "" "b" "" "c" ""

That's why I think 'split into individual characters' should generally mean 'split into non-zero length characters' with the exception of when you're splitting something that is already empty.

from stringr.

hadley avatar hadley commented on May 29, 2024

Ok, that helps. Unfortunately I don't have time to write a fix at the moment, but tests/patches would be appreciated! Does this problem also affect str_split_fixed?

from stringr.

joranE avatar joranE commented on May 29, 2024

It does effect str_split_fixed, in the sense that it also assumes that every string has a leading empty string:

str_split_fixed("abc","",4)
     [,1] [,2] [,3] [,4]
[1,] ""   "a"  "b"  "c" 

Again, I think this is mainly confusing because ?str_split_fixed also says that splitting on "" will split into single characters. At the moment, str_split_fixed behaves like this:

> str_split_fixed("abc","",1)
     [,1] 
[1,] "abc"
> str_split_fixed("abc","",2)
     [,1] [,2] 
[1,] ""   "abc"
> str_split_fixed("abc","",3)
     [,1] [,2] [,3]
[1,] ""   "a"  "bc"
> str_split_fixed("abc","",4)
     [,1] [,2] [,3] [,4]
[1,] ""   "a"  "b"  "c" 

where I would expect either that pattern = "" would override n completely and always force a split into "a" "b" "c". Or possibly something more subtle like this:

> str_split_fixed("abc","",1)
     [,1] 
[1,] "abc"
> str_split_fixed("abc","",2)
     [,1] [,2] 
[1,] "a"   "bc"
> str_split_fixed("abc","",3)
     [,1] [,2] [,3]
[1,] "a"   "b"  "c"
> str_split_fixed("abc","",4)
     [,1] [,2] [,3] [,4]
[1,] "a"   "b"  "c"  "" 

from stringr.

hadley avatar hadley commented on May 29, 2024

Fixed in stringi branch

from stringr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.