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