Skip to content

Find the index of a column by name

Description

Find the index of a column by name

Usage

<DataFrame>$get_column_index(name)

Arguments

name Name of the column to find.

Value

Numeric value (0-indexed) indicating the index of the column

Examples

library("polars")

df <- pl$DataFrame(foo = 1:3, bar = 4:6, ham = c("a", "b", "c"))
df$get_column_index("ham")
#> [1] 2
tryCatch(
  df$get_column_index("sandwich"),
  error = function(e) print(e)
)
#> <error/rlang_error>
#> Error in `df$get_column_index()`:
#> ! Evaluation failed in `$get_column_index()`.
#> Caused by error:
#> ! Column(s) not found: "sandwich" not found
#> ---
#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(df$get_column_index("sandwich"), error = function(e) print(e))
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   4. │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. └─df$get_column_index("sandwich")
#>   6.   ├─polars:::wrap(self$`_df`$get_column_index(name)) at neo-r-polars/R/dataframe-frame.R:315:3
#>   7.   │ └─rlang::try_fetch(...) at neo-r-polars/R/utils-wrap.R:3:3
#>   8.   │   ├─base::tryCatch(...)
#>   9.   │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  10.   │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  11.   │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  12.   │   └─base::withCallingHandlers(...)
#>  13.   └─self$`_df`$get_column_index(name) at neo-r-polars/R/utils-wrap.R:3:3