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

datawizard versions of separate() and unite() #423

Closed
jmgirard opened this issue May 25, 2023 · 3 comments · Fixed by #431
Closed

datawizard versions of separate() and unite() #423

jmgirard opened this issue May 25, 2023 · 3 comments · Fixed by #431
Assignees

Comments

@jmgirard
Copy link

I'm hoping to retool my packages soon to use datawizard instead of dplyr and tidyr. However, a few things I need that don't seem to exist yet are datawizard versions of separate() and unite() from tidyr. Any plans to create these?

@etiennebacher
Copy link
Member

etiennebacher commented May 25, 2023

I think unite() should be fairly easy to implement, but separate() is a bit trickier because of the arguments fill and extra. I started working on implementing separate() in poorman (cf nathaneastwood/poorman#107) but it's still WIP (and I don't know when I'll get back at it). It might provide a starting point if someone else wants to do it.

@strengejacke
Copy link
Member

strengejacke commented May 25, 2023

library(datawizard)

d <- data.frame(
  x = c(NA, 1:3),
  y = c(letters[1:3], NA_character_),
  z = 6:9,
  m = c("X", NA_character_, "Y", "Z")
)
d
#>    x    y z    m
#> 1 NA    a 6    X
#> 2  1    b 7 <NA>
#> 3  2    c 8    Y
#> 4  3 <NA> 9    Z

data_unite(d, new_column = "xyz")
#>         xyz
#> 1  NA_a_6_X
#> 2  1_b_7_NA
#> 3   2_c_8_Y
#> 4  3_NA_9_Z
data_unite(d, new_column = "xyz", remove_na = TRUE)
#>        xyz
#> 1    a_6_X
#> 2    1_b_7
#> 3  2_c_8_Y
#> 4    3_9_Z

data_unite(d, new_column = "x")
#>           x
#> 1  NA_a_6_X
#> 2  1_b_7_NA
#> 3   2_c_8_Y
#> 4  3_NA_9_Z
data_unite(d, new_column = "x", remove_na = TRUE)
#>          x
#> 1    a_6_X
#> 2    1_b_7
#> 3  2_c_8_Y
#> 4    3_9_Z

data_unite(d, new_column = "x", append = TRUE)
#> The name for `new_column` already exists as variable name in the data.
#>   This variable will be replaced by `new_column`.
#>           x    y z    m
#> 1  NA_a_6_X    a 6    X
#> 2  1_b_7_NA    b 7 <NA>
#> 3   2_c_8_Y    c 8    Y
#> 4  3_NA_9_Z <NA> 9    Z
data_unite(d, new_column = "x", remove_na = TRUE, append = TRUE)
#> The name for `new_column` already exists as variable name in the data.
#>   This variable will be replaced by `new_column`.
#>          x    y z    m
#> 1    a_6_X    a 6    X
#> 2    1_b_7    b 7 <NA>
#> 3  2_c_8_Y    c 8    Y
#> 4    3_9_Z <NA> 9    Z

data_unite(d, new_column = "xyz", append = TRUE)
#>    x    y z    m       xyz
#> 1 NA    a 6    X  NA_a_6_X
#> 2  1    b 7 <NA>  1_b_7_NA
#> 3  2    c 8    Y   2_c_8_Y
#> 4  3 <NA> 9    Z  3_NA_9_Z
data_unite(d, new_column = "xyz", remove_na = TRUE, append = TRUE)
#>    x    y z    m      xyz
#> 1 NA    a 6    X    a_6_X
#> 2  1    b 7 <NA>    1_b_7
#> 3  2    c 8    Y  2_c_8_Y
#> 4  3 <NA> 9    Z    3_9_Z

data_unite(d, new_column = "x2")
#>          x2
#> 1  NA_a_6_X
#> 2  1_b_7_NA
#> 3   2_c_8_Y
#> 4  3_NA_9_Z
data_unite(d, select = c("x", "z"), new_column = "new")
#>      y    m  new
#> 1    a    X NA_6
#> 2    b <NA>  1_7
#> 3    c    Y  2_8
#> 4 <NA>    Z  3_9
data_unite(d, select = c("x", "z"), new_column = "new", append = TRUE)
#>    x    y z    m  new
#> 1 NA    a 6    X NA_6
#> 2  1    b 7 <NA>  1_7
#> 3  2    c 8    Y  2_8
#> 4  3 <NA> 9    Z  3_9
data_unite(d, select = c("x", "z"), new_column = "new", append = TRUE, remove_na = TRUE)
#>    x    y z    m new
#> 1 NA    a 6    X   6
#> 2  1    b 7 <NA> 1_7
#> 3  2    c 8    Y 2_8
#> 4  3 <NA> 9    Z 3_9

data_unite(d, new_column = "xyz", separator = ".")
#>         xyz
#> 1  NA.a.6.X
#> 2  1.b.7.NA
#> 3   2.c.8.Y
#> 4  3.NA.9.Z

Created on 2023-05-25 with reprex v2.0.2

@strengejacke
Copy link
Member

@jmgirard data_unite() is implemented, feel free to test. data_separate() will follow in a separate (haha) PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants