# # Unit 8 - Data frames # # Producing data frames by combining vectors # # Two important changes in our work. # First change: dimensions of data structure. # Vector is a one-dimensional collection id <- c(1, 2, 3, 4) gender <- c(2, 2, 1, 2) educ <- c(12, 12, 15, 10) salary <- c(11912.44, 10753.82, 5113.94, 8883.05) # In R we can store data in # two-dimensional collections. # data frame is one type of such collections. # One way to produce it: # The function data.frame() employees <- data.frame(id, gender, educ, salary) print(employees) # Much of our discussion will be # devoted to manipulations in this # data structure. # Many of these manipulations will # be carried out using the library dplyr. print(employees) print(class(employees)) # Note that in principle # the combined vectors must have # the same length. employees <- data.frame(id, 1:99) # In principle - because # in some cases R find # suitable solutions. employees <- data.frame(id, 1:100) print(employees) # Other ways to combine vectors # cbind() - combine as columns employees <- cbind(id, gender, educ, salary) print(employees) print(class(employees)) newEmployees <- data.frame(employees) print(newEmployees) print(class(newEmployees)) # rbind() - combine as rows id <- c(1, 2, 3, 4) gender <- c(2, 2, 1, 2) educ <- c(12, 12, 15, 10) salary <- c(11912.44, 10753.82, 5113.94, 8883.05) employees <- rbind(id, gender, educ, salary) print(employees) print(class(employees)) newEmployees <- data.frame(employees) print(newEmployees) print(class(newEmployees)) # Second change: source of data. # So far the data was provided by us. id <- c(1, 2, 3, 4) gender <- c(2, 2, 1, 2) educ <- c(12, 12, 15, 10) salary <- c(11912.44, 10753.82, 5113.94, 8883.05) # From now on, the data will also # be received from an external source, # specifically files.