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

data_arrange() gets a by argument #564

Closed
wants to merge 9 commits into from
Closed

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Nov 24, 2024

library(datawizard)
data_arrange(head(mtcars), c("gear", "carb"))
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4

data_arrange(head(mtcars), c("gear", "carb"), by = "cyl")
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

Created on 2024-11-24 with reprex v2.1.1

@strengejacke strengejacke marked this pull request as ready for review November 24, 2024 20:15
@etiennebacher
Copy link
Member

etiennebacher commented Nov 24, 2024

I don't understand the example, isn't it the same as data_arrange(head(mtcars), c("cyl", "gear", "carb"))?

identical(
  data_arrange(head(mtcars), c("gear", "carb"), by = "cyl"),
  data_arrange(head(mtcars), c("cyl", "gear", "carb"))
)
#> TRUE

@strengejacke
Copy link
Member Author

strengejacke commented Nov 24, 2024

Bad example. This should be better:

library(datawizard)
set.seed(123)
x <- iris[sample(seq_len(nrow(iris)), 10, replace = TRUE), c("Sepal.Width", "Species")]
rownames(x) <- NULL

data_arrange(x, c("Sepal.Width", "Species"))
#>    Sepal.Width    Species
#> 1          2.5 versicolor
#> 2          2.6 versicolor
#> 3          2.6 versicolor
#> 4          3.0     setosa
#> 5          3.0     setosa
#> 6          3.0 versicolor
#> 7          3.2     setosa
#> 8          3.3     setosa
#> 9          3.8  virginica
#> 10         3.8  virginica

data_arrange(x, "Sepal.Width", by = "Species")
#>    Sepal.Width    Species
#> 1          3.0     setosa
#> 2          3.0     setosa
#> 3          3.2     setosa
#> 4          3.3     setosa
#> 5          2.5 versicolor
#> 6          2.6 versicolor
#> 7          2.6 versicolor
#> 8          3.0 versicolor
#> 9          3.8  virginica
#> 10         3.8  virginica

Created on 2024-11-24 with reprex v2.1.1

@etiennebacher
Copy link
Member

The by value must come first in the select argument, those are equivalent:

identical(
  data_arrange(x, "Sepal.Width", by = "Species"),
  data_arrange(x, c("Species", "Sepal.Width"))
)
#> TRUE

@strengejacke
Copy link
Member Author

right! How did I miss that? Ok, then this can be closed.

@strengejacke strengejacke deleted the data_arrange_by branch November 24, 2024 21:17
@IndrajeetPatil IndrajeetPatil removed their request for review November 24, 2024 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants