-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.of.variable.R
More file actions
38 lines (37 loc) · 1.01 KB
/
Copy pathindex.of.variable.R
File metadata and controls
38 lines (37 loc) · 1.01 KB
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
#' Get Column Index
#'
#' @name index.of.variable
#'
#' Returns column index by name.
#'
#' @param varname Variable name.
#' @param df Data frame.
#'
#' @return Integer index.
#'
#' @examples
#' df <- data.frame(firm = 1:3, date = 4:6, ret = 7:9)
#'
#' # Find the column position of "ret"
#' iaw$index.of.variable("ret", df) # 3
#'
#' # Use the index for programmatic subsetting
#' idx <- iaw$index.of.variable("date", df)
#' df[, idx] # 4 5 6
#'
#' # Returns integer(0) when variable is missing
#' iaw$index.of.variable("price", df) # integer(0)
#'
#' # Locate a column among many in a wide financial dataset
#' wide <- data.frame(permno = 1, yyyymm = 2, ret = 3, vol = 4, mktcap = 5)
#' iaw$index.of.variable("mktcap", wide) # 5
#'
#' # Use with column reordering: move "ret" to the first position
#' idx <- iaw$index.of.variable("ret", df)
#' df[, c(idx, setdiff(seq_along(df), idx))]
#'
#' @family utilities
#' @export
iaw$index.of.variable <- function(varname, df) {
which(names(df) == varname)
}