# # Unit 8 - Data frames # # Reading a data frame from a .csv file, # and the $ operator # # Our goal: to read data from a file # into the present session. # Specifically: a .csv file # More specifically: employees. csv # Note: the extension ".csv" is necessary. employees <- read.csv("employees.csv") # Setting the working directory # Rules: # - Name: only English letters and digits # - No spaces # - No Hebrew # - Not on the cloud # - Not in Documents # - Not in Desktop setwd("c:/R") print(getwd()) # Possible errors setwd("c:/AR") setwd("c:\AR") # Now it should work # Inspect the Environment. employees <- read.csv("employees.csv") print(employees) View(employees) # Now that we have the data frame # in hand, we may access each of its # columns using the $ operator gender <- employees$GENDER print(gender) print(class(gender)) print(employees$GENDER) print(class(employees$GENDER)) # Note, if you do not store # a column/variable in a specific # variable, you must indicate # the data frame to which the column # belongs. print(employees$SALARY) print(employees$GENDER[employees$GENDER == 1]) # Also, the indicated column # of course must exist in the data frame... print(employees$SALARY) print(employees$SALARY1) print(employees$salary) print(employees$SALARY) # With the a column in hand # as a vector, we may carry out # operations in the vector level. print(length(employees$GENDER[employees$GENDER == 1]))