move_columns: Move columns to other positions in a data frame

Description Usage Arguments Value Note Examples

View source: R/move_column.R

Description

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

Usage

1
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.

Note

If neither .before nor .after are specified, the column is moved to the end of the data frame by default. .before and .after are evaluated in a non-standard fashion, so you need quasi-quotation when the value for .before or .after is a vector with the target-column value. See 'Examples'.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## 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()
## End(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()

Example output

  Sepal.Length Petal.Length Petal.Width Species Sepal.Width
1          5.1          1.4         0.2  setosa         3.5
2          4.9          1.4         0.2  setosa         3.0
3          4.7          1.3         0.2  setosa         3.2
4          4.6          1.5         0.2  setosa         3.1
5          5.0          1.4         0.2  setosa         3.6
6          5.4          1.7         0.4  setosa         3.9
  Sepal.Width Sepal.Length Petal.Length Petal.Width Species
1         3.5          5.1          1.4         0.2  setosa
2         3.0          4.9          1.4         0.2  setosa
3         3.2          4.7          1.3         0.2  setosa
4         3.1          4.6          1.5         0.2  setosa
5         3.6          5.0          1.4         0.2  setosa
6         3.9          5.4          1.7         0.4  setosa
  Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1  setosa          5.1         3.5          1.4         0.2
2  setosa          4.9         3.0          1.4         0.2
3  setosa          4.7         3.2          1.3         0.2
4  setosa          4.6         3.1          1.5         0.2
5  setosa          5.0         3.6          1.4         0.2
6  setosa          5.4         3.9          1.7         0.4
  Sepal.Length Species Petal.Length Sepal.Width Petal.Width
1          5.1  setosa          1.4         3.5         0.2
2          4.9  setosa          1.4         3.0         0.2
3          4.7  setosa          1.3         3.2         0.2
4          4.6  setosa          1.5         3.1         0.2
5          5.0  setosa          1.4         3.6         0.2
6          5.4  setosa          1.7         3.9         0.4

Attaching package:dplyrThe following objects are masked frompackage:stats:

    filter, lag

The following objects are masked frompackage:base:

    intersect, setdiff, setequal, union

  Sepal.Length Petal.Length Species Sepal.Width Petal.Width
1          5.1          1.4  setosa         3.5         0.2
2          4.9          1.4  setosa         3.0         0.2
3          4.7          1.3  setosa         3.2         0.2
4          4.6          1.5  setosa         3.1         0.2
5          5.0          1.4  setosa         3.6         0.2
6          5.4          1.7  setosa         3.9         0.4
  Sepal.Length Petal.Length Petal.Width Species Sepal.Width
1          5.1          1.4         0.2  setosa         3.5
2          4.9          1.4         0.2  setosa         3.0
3          4.7          1.3         0.2  setosa         3.2
4          4.6          1.5         0.2  setosa         3.1
5          5.0          1.4         0.2  setosa         3.6
6          5.4          1.7         0.4  setosa         3.9
  Sepal.Length Petal.Length Petal.Width Sepal.Width Species
1          5.1          1.4         0.2         3.5  setosa
2          4.9          1.4         0.2         3.0  setosa
3          4.7          1.3         0.2         3.2  setosa
4          4.6          1.5         0.2         3.1  setosa
5          5.0          1.4         0.2         3.6  setosa
6          5.4          1.7         0.4         3.9  setosa

sjmisc documentation built on Dec. 11, 2021, 9:34 a.m.