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

Implement stringr::str_replace_na() #132

Open
etiennebacher opened this issue Aug 13, 2024 · 0 comments
Open

Implement stringr::str_replace_na() #132

etiennebacher opened this issue Aug 13, 2024 · 0 comments
Labels
feature good first issue Good for newcomers

Comments

@etiennebacher
Copy link
Owner

What functionality are you missing?
https://stringr.tidyverse.org/reference/str_replace_na.html

Is this functionality present in the tidyverse or in polars (or both)?
stringr


I didn't even know this existed, so I'm unlikely to implement this myself but I think it's a good first issue if someone wants to contribute using the steps detailed in this guide.

Here's the equivalent in Polars (those are simple cases, it would be worth checking the stringr tests for corner cases):

library(polars)
library(stringr)
library(dplyr, warn.conflicts = FALSE)

test_df <- data.frame(x = c(NA, "abc", "def"), y = c(NA, 2, 3))
test_pl <- as_polars_df(test_df)

### tidyverse
# basic
test_df |> 
  mutate(foo = str_replace_na(x))
#>      x  y foo
#> 1 <NA> NA  NA
#> 2  abc  2 abc
#> 3  def  3 def
# coerce values to character
test_df |> 
  mutate(foo = str_replace_na(y))
#>      x  y foo
#> 1 <NA> NA  NA
#> 2  abc  2   2
#> 3  def  3   3

### polars
# basic
test_pl$with_columns(
  foo = pl$col("x")$replace_strict(
    old = NA, new = "NA", default = pl$col("x"), return_dtype = pl$String
  )
)
#> shape: (3, 3)
#> ┌──────┬──────┬─────┐
#> │ x    ┆ y    ┆ foo │
#> │ ---  ┆ ---  ┆ --- │
#> │ str  ┆ f64  ┆ str │
#> ╞══════╪══════╪═════╡
#> │ null ┆ null ┆ NA  │
#> │ abc  ┆ 2.0  ┆ abc │
#> │ def  ┆ 3.0  ┆ def │
#> └──────┴──────┴─────┘
# coerce values to character
test_pl$with_columns(
  foo = pl$col("y")$replace_strict(
    old = NA, new = "NA", default = pl$col("y"), return_dtype = pl$String
  )
)
#> shape: (3, 3)
#> ┌──────┬──────┬─────┐
#> │ x    ┆ y    ┆ foo │
#> │ ---  ┆ ---  ┆ --- │
#> │ str  ┆ f64  ┆ str │
#> ╞══════╪══════╪═════╡
#> │ null ┆ null ┆ NA  │
#> │ abc  ┆ 2.0  ┆ 2.0 │
#> │ def  ┆ 3.0  ┆ 3.0 │
#> └──────┴──────┴─────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant