I have a string, "1500|3|10000|5"
and I wish to have a numeric vector like so:
[1] 1500 3 10000 5
strsplit is much faster than str_extract_all
. Is strsplit the fastest way to do this?
library("tidyverse")
library("microbenchmark")
x <- "1500|3|10000|5"
# mean ~ 137 microseconds
microbenchmark(
x |>
str_extract_all("\\d+") |>
unlist(use.names = FALSE) |>
as.double()
)
# mean ~ 15 microseconds
microbenchmark(
x |>
strsplit(split = "\\|") |>
unlist(use.names = FALSE) |>
as.double()
)