Skip to contents

Create Function to Safely Access Data Frame Columns or List Elements

Usage

createAccessor(x)

Arguments

x

a data frame or a list

Examples

getcol <- createAccessor(data.frame(a = 1:2, b = 2:3))
getcol("a")
#> [1] 1 2
getcol("b")
#> [1] 2 3
getcol(c("b", "a"))
#>   b a
#> 1 2 1
#> 2 3 2
# getcol("c") # error with info about existing columns