Skip to contents

Insert one or more columns into an existing data frame.

Usage

add_column(data, x, before = NULL, after = NULL)

Arguments

data

A data frame.

x

A named list of values to insert. All values must have length nrow(data), or be of length 1.

before, after

An integer vector for the column index or character vector for the column name of where to add the new columns. Defaults to after last column.

Value

data.frame

References

Kirill Muller, Hadley Wickham, and Romain francois (Rstudio) (2017). tibble: Simple Data Frames. R package version 1.4.2. https://CRAN.R-project.org/package=tibble. Git commit 294c3cffad38f8f9f277c848742a90984fe239e3

Examples

#----------------------------------------------------------------------------
# add_column() examples
#----------------------------------------------------------------------------
library(bkdat)

df <- data.frame(x = 1:3, y = 3:1)
add_column(df, list(z = -1:1, w = 0))
#>   x y  z w
#> 1 1 3 -1 0
#> 2 2 2  0 0
#> 3 3 1  1 0

if (FALSE) { # \dontrun{
# You can't overwrite existing columns
add_column(df, list(x = 4:6))

# You can't increase the number of rows
add_column(df, list(z = 1:5))
} # }