Learn R Programming

berryFunctions (version 1.15.0)

getColumn: get column from data.frame

Description

(Try to) extract a column from a data frame with USEFUL warnings/errors. Watch out not to define objects with the same name as x if you are using getColumn in a function!

Usage

getColumn(x, df, trace = TRUE)

Arguments

x
Column name to be subsetted. The safest is to use character strings or substitute(input). If there is an object "x" in a function environment, its value will be used as name! (see upper2 example)
df
dataframe object
trace
Logical: Add function call stack to the message? DEFAULT: TRUE WARNING: in do.call settings with large objects, tracing may take a lot of computing time.

Value

Vector (or array, factor, etc) with values in the specified column

See Also

subset, getElement, https://mran.revolutionanalytics.com/web/packages/car/vignettes/embedding.pdf

Examples

Run this code
getColumn(Air.Flow, stackloss)
getColumn(2, stackloss)
getColumn("2",  stackloss) # works too...
getColumn(2,  stackloss[0,])
# The next examples all return errors:
try( getColumn(2,  stackloss[0])  )
try( getColumn(2,  stackloss[,0]) )
try( getColumn(Acid, stackloss)   ) # design choice: partial matching not supported
try( getColumn(2:3,  stackloss)   ) # cannot be a vector
try( getColumn(c("Air.Flow","Acid.Conc"),  stackloss)    )

upper <- function(x, select) getColumn(x, stackloss[select,])
upper(Water.Temp)
upper(2)
upper(2, select=0)
# upper(Water)  # error with useful message

# Pitfall lexical scoping: R only goes up until it finds things:
upper2 <- function(xx) {xx <- "Timmy!"; getColumn(xx, stackloss)} # will break!
is.error(      upper2(Water.Temp) ,   force=TRUE, tell=TRUE) # is an error

upper3 <- function(xx, dd) getColumn(substitute(xx), dd)
upper3(Air.Flow, stackloss) # may be safer in many scoping situations

# In packages use "colname" with quotation marks in level 2 functions to avoid 
# the CRAN check NOTE "no visible binding for global variable"

df <- data.frame(x=letters[1:3],y=letters[4:6]) 
is.vector(df$x)
is.vector(getColumn("x", df)) # FALSE
# cannot force output to be a vector, as this will convert:
as.Date("2016-09-14")  ;  as.vector(as.Date("2016-09-14"))
# same problem with dfs from tapply results
# better ideas welcome!! (berry-b@gmx.de)

# Pitfall numerical column names:
df <- data.frame(1:5, 3:7)
colnames(df) <- c("a","1") # this is a bad idea anyways
getColumn("1", df) # will actually return the first column, not column "1"

getColumn(1, data.frame(AA=rep(NA,10)))

Run the code above in your browser using DataLab