insertColumns: Insert new Column(s) into a Data Frame

View source: R/column.R

insertColumnsR Documentation

Insert new Column(s) into a Data Frame

Description

Insert one or more new columns into a data frame before or after the given column

Usage

insertColumns(
  Data,
  ...,
  before = "",
  after = "",
  stringsAsFactors = defaultIfNULL(options()$stringsAsFactors, FALSE)
)

Arguments

Data

data frame

...

named objects each of which will be a new column in the data frame. Each object must have as many elements as Data has rows.

before

name of column before which to insert the new column(s)

after

name of column after which to insert the new column(s)

stringsAsFactors

passed on to data.frame() and cbind()

Value

data frame Data with new columns inserted before the column named as given in before or after the column named as given in after

Examples

Data <- data.frame(A = 1:5, B = 2:6)

# Insert new columns X and Y before column "B"
insertColumns(Data, before = "B", X = paste0("x", 1:5), Y = paste0("y", 1:5))

# This is the same as inserting new columns X and Y after column "A":
insertColumns(Data, after = "A", X = paste0("x", 1:5), Y = paste0("y", 1:5))

# You may also insert before the first...
insertColumns(Data, before = "A", X = paste0("x", 1:5), Y = paste0("y", 1:5))

# ... or after the last column
insertColumns(Data, after = "B", X = paste0("x", 1:5), Y = paste0("y", 1:5))
  

KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.