stringr icon indicating copy to clipboard operation
stringr copied to clipboard

Doc clarification for str_subset about dropping names.

Open Ax3man opened this issue 2 years ago • 1 comments

str_subset drops names. This appears to a design choice from stringi (https://github.com/gagolews/stringi/issues/59). Fair enough.

But I got bitten by this today, as I read the documentation stating: "It's a wrapper around x[str_detect(x, pattern)], and is equivalent to grep(pattern, x, value = TRUE)." But both those alternatives do keep names, and there is no other mention of dropping attributes. I suggest to add "but without preserving attributes like names", or something similar.

Consider:

fruit <- c(A = "apple", B = "banana", C = "pear", D = "pineapple")
str_subset(fruit, "b")
fruit[str_detect(fruit, 'b')]
grep('b', fruit, value = TRUE)

Ax3man avatar Apr 18 '23 19:04 Ax3man

I think we can just fix the behaviour.

hadley avatar Aug 04 '23 19:08 hadley

Looks like the easiest way to fix this would be to switch to making str_subset() use [ + str_detect().

hadley avatar Jul 15 '24 21:07 hadley