-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_col.R
More file actions
27 lines (27 loc) · 745 Bytes
/
add_col.R
File metadata and controls
27 lines (27 loc) · 745 Bytes
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
#' Add a colum to a data frame
#'
#' Similar to cbind, but allows you to specify the positoin. Will replac
#' existing variable with the same name if present.
#'
#' @param x A data frame
#' @param name Name of variabl to create. If variable of that name
#' already exists
#' @param value Values to insert.
#' @param where position to insert. Use 1 to insert on LHS.
#' RHS.
#' @export
#' @examples
#' df <- data.frame(x = 1:5)
#' add_col(df, "y", runif(5))
#' add_col(df, "y", runif(5), where = 1)
#'
#' add_col(df, "x", 5:1)
add_col <- function(x, name, value, where = ncol(x) + 1) {
if (name %in% names(x)) {
x[[name]] <- value
x
} else {
df <- setNames(data.frame(value), name)
insert_into(x, df, where = where)
}
}