Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: sep argument in str_dup. #487

Open
maellecoursonnais opened this issue Dec 15, 2022 · 0 comments
Open

Feature request: sep argument in str_dup. #487

maellecoursonnais opened this issue Dec 15, 2022 · 0 comments
Labels
feature a feature request or enhancement

Comments

@maellecoursonnais
Copy link

I was going through this SO question, and thought that it might be useful to add an optional sep argument to str_dup.

For instance, in the case of the SO question the OP wants to repeat a string and add ; between every repeated value.

n = 3
input = c(67L, 24L, 72L, 3L)
expected_output = c("67;67;67", "24;24;24", "72;72;72", "3;3;3")

So far, what one can do is:

library(stringr)
library(purrr)
map_chr(input, ~ str_flatten(rep(.x, each = n), ";"))
#[1] "67;67;67" "24;24;24" "72;72;72" "3;3;3"

Or in base R:

sapply(input, \(x) paste(rep(x, n), collapse=";")) 

But what about a simple str_dup call?

str_dup(input, n, sep = ";")
@hadley hadley added the feature a feature request or enhancement label Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants