Skip to contents

Add a (Named) Vector as a Column

Usage

append_column(x, ...)

# S4 method for class 'data.frame'
append_column(x, column, after = 0, var = ".col")

Arguments

x

A data.frame.

...

Currently not used.

column

A (named) vector.

after

A length-one numeric vector specifying a subscript, after which the new column is to be appended.

var

A character string giving the name of the new column.

Value

A data.frame.

Details

If column is named, names will be matched to the row names of x. Only the first match is retained, and elements of column without a match are removed. This allows to add as a column a vector whose length is less than the number of rows in x (NAs will be inserted).

See also

Other data preparation tools: append_rownames(), assign(), compact(), count(), detect(), discard(), get(), keep(), seek()

Author

N. Frerebeau

Examples

X <- data.frame(
  x = 1:5,
  y = 6:10,
  row.names = LETTERS[1:5]
)

Y <- c(D = 44, B = 55, Z = 22)

append_column(X, Y, after = 3)
#>   x  y .col
#> A 1  6   NA
#> B 2  7   55
#> C 3  8   NA
#> D 4  9   44
#> E 5 10   NA