/
pull.R
55 lines (48 loc) · 1.04 KB
/
pull.R
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#' Pull out a single variable
#'
#' @description
#' Pull a single variable from a data.table as a vector.
#'
#' @param .df A data.frame or data.table
#' @param var The column to pull from the data.table as:
#' * a variable name
#' * a positive integer giving the column position
#' * a negative integer giving the column position counting from the right
#' @param name Optional - specifies the column to be used as names for the vector.
#'
#' @export
#'
#' @examples
#' df <- data.table(
#' x = 1:3,
#' y = 1:3
#' )
#'
#' # Grab column by name
#' df %>%
#' pull(y)
#'
#' # Grab column by position
#' df %>%
#' pull(1)
#'
#' # Defaults to last column
#' df %>%
#' pull()
pull <- function(.df, var = -1, name = NULL) {
vec <- .pull(.df, {{ var }})
name <- enquo(name)
if (!quo_is_null(name)) {
names(vec) <- .pull(.df, !!name)
}
vec
}
.pull <- function(.df, var) {
vars <- as.list(seq_along(.df))
names(vars) <- names(.df)
var <- eval_tidy(enquo(var), vars)
if (var < 0) {
var <- length(vars) + var + 1
}
.df[[var]]
}