Learn R Programming

mark (version 0.8.2)

reindex: Reindex a data.frame

Description

Reindexes a data.frame with a reference

Usage

reindex(
  x,
  index = NULL,
  new_index,
  expand = c("intersect", "both"),
  sort = FALSE
)

Value

A data.frame with rows of index

Arguments

x

A data.frame

index

The column name or number of an index to use; if NULL will assume the first column; a value of row.names will use row.names(x)

new_index

A column vector of the new index value

expand

Character switch to expand or keep only the values that intersect (none), all values in x or index, or retain all values found.

sort

Logical, if TRUE will sort the rows in output

Examples

Run this code
iris1 <- head(iris, 5)
iris1$index <- 1:5
reindex(iris1, "index", seq(2, 8, 2))
reindex(iris1, "index", seq(2, 8, 2), expand = "both")

# Using letters will show changes in rownames
iris1$index <- letters[1:5]
reindex(iris1, "index", letters[seq(2, 8, 2)])
reindex(iris1, "index", seq(2, 8, 2))
reindex(iris1, "index", seq(2, 8, 2), expand = "both")

Run the code above in your browser using DataLab