# # Unit 9 - Data frames # # Using data frames argumens in user-functions # # Loading data setwd("c:/R") employees <- read.csv("employees.csv") print(employees) # Data frames may be passed to a user-function foo <- function(df) { print(df) } foo(employees) goo <- function(df) { return (dim(df)) } print(goo(employees)) # Seems pretty obvious. # Yet there is one aspect # of this issue which is # not at all obvious. # Inspect the following: foo <- function(df, colName) { print(df$colName) } foo(employees, "GENDER") # That is strange, because # given this specific call to foo, # the above function is # allegedly equivalent to the following, # which works: foo <- function(df) { print(df$"GENDER") } foo(employees) # However, the codes in fact # are not equivalent. An equivalent # coude would be the follwing, # which does not work. foo <- function(df) { colName <- "Gender" print(df$colName) } foo(employees) # To understand the problem we should # delve into the work of the dollar operator. # it passes the argument name as a character string, # therefore employees$colName is interpreted as # employees$"colName". # Inspect the following code: foo <- function(df, colName) { print(df$colName) } employees$colName <- 5 foo(employees, "GENDER") # So how would we access, a certain # variable in a data frame argument, using # a parameter holding the column's name? # The [ ] comes to our rescue foo <- function(df, colName) { print(df[,colName]) } foo(employees, "GENDER")