Rearrange Rows by Variables
rearrange.Rd
Rearrange (reorder) and possibly filter the rows
of a data frame or tibble object according to a
matched vector. Similar to dplyr::filter()
and dplyr::arrange()
, however the
ordering is based on an external object x
,
rather than a variable contained in tbl
.
Details
This function is meant to act as a working solution to
the lack of row names inherent to tibble objects, yet
still plays nice with tidyverse
syntax.
Values in x
that are not in tbl[[var]]
are silently dropped. Values in tbl[[var]]
that are
not in x
are silently filtered.
Duplicates in x
are silently dropped to avoid
creating extra rows in tbl
, whereas duplicates
in var
are maintained.
Examples
df <- tibble::tibble(name = letters[1:4L], x = 1:4, y = rnorm(4))
df
#> # A tibble: 4 × 3
#> name x y
#> <chr> <int> <dbl>
#> 1 a 1 0.229
#> 2 b 2 0.622
#> 3 c 3 0.115
#> 4 d 4 1.50
rearrange(df, "name", c("c", "b", "d"))
#> # A tibble: 3 × 3
#> name x y
#> <chr> <int> <dbl>
#> 1 c 3 0.115
#> 2 b 2 0.622
#> 3 d 4 1.50
rearrange(df, "name", "d")
#> # A tibble: 1 × 3
#> name x y
#> <chr> <int> <dbl>
#> 1 d 4 1.50
rearrange(df, "name", c("a", "c", "z")) # "z" is ignored
#> # A tibble: 2 × 3
#> name x y
#> <chr> <int> <dbl>
#> 1 a 1 0.229
#> 2 c 3 0.115
rearrange(df, "name", c("a", "c", "c")) # duplicate "c" is dropped
#> # A tibble: 2 × 3
#> name x y
#> <chr> <int> <dbl>
#> 1 a 1 0.229
#> 2 c 3 0.115