reindex: Re-index

Description Usage Arguments Details Examples

View source: R/reindex.R

Description

Re-index a column based on a new index

Usage

1
reindex(x, index, new_index, keep_all = FALSE, ...)

Arguments

x

A data.frame or named vector

index

The name of the column to reindex by (see details)

new_index

The new order of the index as a vector

keep_all

Logical, whether to use only the index values in the 'new_index' or to use all available index values

...

Additional arguments passed to methods

Details

When implementing the 'keep_all' argument, NA values in the same class as the data.frame (df) are assigned. The 'index' and 'new_index' must be not have any duplicates. If a vector is passed, the 'index' argument is not necessary. However, if this argument is set and 'new_index' is missing, the latter will take the value of the former.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
iris1 <- head(iris, 5)
iris1$index <- 1:5
reindex(iris1, "index", seq(2, 8, 2))
reindex(iris1, "index", seq(2, 8, 2), keep_all = TRUE)

x <- c("a" = 1, "b" = 2, "d" = 4)
y <- setNames(nm = letters[2:5])
reindex(x, new_index = y )
reindex(x, new_index = y, keep_all = TRUE)

x <- 2:5
index <- NULL
names(x) <- letters[x]
new_index <- c(1, 3, 5, 7)
names(new_index) <- letters[new_index]
reindex(x, new_index = new_index)
reindex(x, new_index = new_index, keep_all = TRUE)

reindex(setNames(nm = letters[2:4]), letters[1:5])

jmbarbone/dirtyr documentation built on Sept. 23, 2020, 4:05 a.m.