etiennebacher / tidypolars

Get the power of polars with the syntax of the tidyverse
https://tidypolars.etiennebacher.com
Other
179 stars 4 forks source link

Implement `separate_longer_*()` and `separate_wider_*()` #57

Open etiennebacher opened 1 year ago

etiennebacher commented 1 year ago

If they become generics one day: https://github.com/tidyverse/tidyr/issues/1534

library(tidypolars)
#> Registered S3 method overwritten by 'tidypolars':
#>   method          from  
#>   print.DataFrame polars
library(polars)
library(tibble)

df <- tibble(id = 1:4, x = c("x", "x y", "x y z", NA))
pl_df <- as_polars_df(df)

df
#> # A tibble: 4 × 2
#>      id x    
#>   <int> <chr>
#> 1     1 x    
#> 2     2 x y  
#> 3     3 x y z
#> 4     4 <NA>
pl_df
#> shape: (4, 2)
#> ┌─────┬───────┐
#> │ id  ┆ x     │
#> │ --- ┆ ---   │
#> │ i32 ┆ str   │
#> ╞═════╪═══════╡
#> │ 1   ┆ x     │
#> │ 2   ┆ x y   │
#> │ 3   ┆ x y z │
#> │ 4   ┆ null  │
#> └─────┴───────┘

tidyr::separate_longer_delim(df, x, delim = " ")
#> # A tibble: 7 × 2
#>      id x    
#>   <int> <chr>
#> 1     1 x    
#> 2     2 x    
#> 3     2 y    
#> 4     3 x    
#> 5     3 y    
#> 6     3 z    
#> 7     4 <NA>

pl_df$with_columns(pl$col("x")$str$split(by = " "))$explode("x")
#> shape: (7, 2)
#> ┌─────┬──────┐
#> │ id  ┆ x    │
#> │ --- ┆ ---  │
#> │ i32 ┆ str  │
#> ╞═════╪══════╡
#> │ 1   ┆ x    │
#> │ 2   ┆ x    │
#> │ 2   ┆ y    │
#> │ 3   ┆ x    │
#> │ 3   ┆ y    │
#> │ 3   ┆ z    │
#> │ 4   ┆ null │
#> └─────┴──────┘