Giter VIP home page Giter VIP logo

Comments (11)

gagolews avatar gagolews commented on May 27, 2024

@hadley's suggestion/question:


x <- c("a b c")
stri_sub(x, c(1, 3), c(1, 3)) <- c("A", "B")
x
# [1] "A b c" "a B c"

Is there an easy way to get "A B c"  ?

from stringi.

gagolews avatar gagolews commented on May 27, 2024

vectorize_all arg to stri_sub???

from stringi.

gagolews avatar gagolews commented on May 27, 2024

@markdanese's example:

# toy data and split matrix
x <- c("abcdefgh", "ijklmnop")
split <- matrix(1:8, ncol = 2, byrow = TRUE)

stri_sub(x, split)
gives:
[1] "ab" "kl" "ef" "op"

but I was hoping for this:

[[1]]
[1] "ab" "cd" "ef" "gh"
[[2]]
[1] "ij" "kl" "mn" "op"

or even better, this:

     [,1] [,2] [,3] [,4]
[1,] "ab" "cd" "ef" "gh"
[2,] "ij" "kl" "mn" "op"

The last piece is accomplished by do.call(rbind, lapply(x, stri_sub, split)) but I was hoping it might be faster if it were all done in c++.

from stringi.

gagolews avatar gagolews commented on May 27, 2024

Note: we should be able to imitate a call to stri_replace(...) with stri_subXXX(stri_locate(...)) <- ... and a call to stri_extract(...) with stri_sub(stri_locate(...))

from stringi.

gagolews avatar gagolews commented on May 27, 2024

Also, stri_paste accepting lists could be handy, see #175

from stringi.

gagolews avatar gagolews commented on May 27, 2024

@hadley's example in #234:

library(stringi)
x <- "abcdefgh"
str_sub(x, c(1, 3), c(1, 3))  <- c("X", "Y")
x
#> [1] "Xbcdefgh" "abYdefgh"

Ideally it would return "XbYdefgh"

from stringi.

hadley avatar hadley commented on May 27, 2024

Ooops, I guess I keep forgetting about this issue!

from stringi.

magic-lantern avatar magic-lantern commented on May 27, 2024

It's been quite some time since the last comment. Is there a work around for this that people have come up with?

from stringi.

gagolews avatar gagolews commented on May 27, 2024

Finally I've had time for this. DONE.

Example 1:

> stri_sub_replace_all("a b c", c(1, 3, 5), c(1, 3, 5), value=c("A", "B", "C"))
[1] "A B C"

Example 2:

> x <- c("12 3456 789", "abc", "", NA, "667")
> stri_sub_all(x, stri_locate_all_regex(x, "[0-9]+", omit_no_match=TRUE)) <- "***"
> print(x)
[1] "*** *** ***" "abc"         ""            NA            "***"  

Example 3 (@markdanese):

> x <- c("abcdefgh", "ijklmnop")
> split <- matrix(1:8, ncol = 2, byrow = TRUE)
> split
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
[4,]    7    8
> stri_sub_all(x, split)
[[1]]
[1] "ab" "cd" "ef" "gh"

[[2]]
[1] "ij" "kl" "mn" "op"
> stri_list2matrix(stri_sub_all(x, split), byrow=TRUE)
     [,1] [,2] [,3] [,4]
[1,] "ab" "cd" "ef" "gh"
[2,] "ij" "kl" "mn" "op"

from stringi.

hadley avatar hadley commented on May 27, 2024

Nice work - thanks!

from stringi.

markdanese avatar markdanese commented on May 27, 2024

thanks! looking forward to testing it out

from stringi.

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.