I'm puzzled as to why I get this behaviour with str_split_fixed in the stringr package.> stringr::str_split_fixed('ab','',2)[,1] [,2] [1,] "" "ab"> stringr::str_split_fixed('ab','',3)[,1] [,2] [,3] [1,] "" "a" "b" In the first example, I was expecting to get [,1] [,2] [1,] "a" "b" Can someone explain? Thanks, David
Dear David, str_split_fixed calls str_locate_all, which gives str_locate_all("ab", "") ## [[1]] ## start end ## [1,] 1 0 ## [2,] 2 1 ## in your example, since "" is a character of length 1. substring() is probably more intuitive to get your expected result: substring("ab", 1:2, 1:2) ## [1] "a" "b" David Barron <dnbarron at gmail.com> schrieb am Wed, 14. Jan 18:47:>I'm puzzled as to why I get this behaviour with str_split_fixed in the >stringr package. > >> stringr::str_split_fixed('ab','',2) > [,1] [,2] >[1,] "" "ab" > >> stringr::str_split_fixed('ab','',3) > [,1] [,2] [,3] >[1,] "" "a" "b" > >In the first example, I was expecting to get > [,1] [,2] >[1,] "a" "b" > >Can someone explain? > >Thanks, >David > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
FWIW this is fixed in the dev version of stringr which uses stringi under the hood:> stringr::str_split_fixed('ab','',2)[,1] [,2] [1,] "a" "b"> stringr::str_split_fixed('ab','',3)[,1] [,2] [,3] [1,] "a" "b" "" Hadley On Wed, Jan 14, 2015 at 12:47 PM, David Barron <dnbarron at gmail.com> wrote:> I'm puzzled as to why I get this behaviour with str_split_fixed in the > stringr package. > >> stringr::str_split_fixed('ab','',2) > [,1] [,2] > [1,] "" "ab" > >> stringr::str_split_fixed('ab','',3) > [,1] [,2] [,3] > [1,] "" "a" "b" > > In the first example, I was expecting to get > [,1] [,2] > [1,] "a" "b" > > Can someone explain? > > Thanks, > David > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- http://had.co.nz/