Learn R Programming

sjmisc (version 2.8.7)

move_columns: Move columns to other positions in a data frame

Description

move_columns() moves one or more columns in a data frame to another position.

Usage

move_columns(data, ..., .before, .after)

Arguments

data

A data frame.

...

Unquoted names or character vector with names of variables that should be move to another position. You may also use functions like : or tidyselect's select-helpers.

.before

Optional, column name or numeric index of the position where col should be moved to. If not missing, col is moved to the position before the column indicated by .before.

.after

Optional, column name or numeric index of the position where col should be moved to. If not missing, col is moved to the position after the column indicated by .after.

Value

data, with resorted columns.

Examples

Run this code
# NOT RUN {
data(iris)

iris %>%
  move_columns(Sepal.Width, .after = "Species") %>%
  head()

iris %>%
  move_columns(Sepal.Width, .before = Sepal.Length) %>%
  head()

iris %>%
  move_columns(Species, .before = 1) %>%
  head()

iris %>%
  move_columns("Species", "Petal.Length", .after = 1) %>%
  head()

library(dplyr)
iris %>%
  move_columns(contains("Width"), .after = "Species") %>%
  head()
# }
# NOT RUN {
# using quasi-quotation
target <- "Petal.Width"
# does not work, column is moved to the end
iris %>%
  move_columns(Sepal.Width, .after = target) %>%
  head()

# using !! works
iris %>%
  move_columns(Sepal.Width, .after = !!target) %>%
  head()
# }

Run the code above in your browser using DataLab