# ============================================================================ # ECONOMETRICS CLASS 2: INTRODUCTION TO R # ============================================================================ # ============================================================================ # PART 1: R BASICS # ============================================================================ # Positron interface: # - Console (bottom): where code runs # - Editor (top): where you write scripts # - Variables pane (right): shows your data and objects # - Plots/Help tabs (right): displays visualizations and documentation # R as a calculator 2 + 2 10 * 5 100 / 4 2^3 sqrt(25) exp(2) log(1) # Creating objects (assignment) x <- 5 y <- 10 z <- x + y z name <- "Ricardo" # An object of type string name # print the object value in the console Name # R is case sensitive # Creating vectors ages <- c(22, 23, 25, 21, 24) ages names <- c("Alice", "Bob", "Charlie", "Diana", "Eve") names # Basic operations on vectors mean(ages) median(ages) sd(ages) sum(ages) length(ages) # Accessing elements ages[1] # First element ages[1:3] # First three elements ages[c(1, 5)] # First and fifth elements # EXERCISE 1: # Create a vector with the grades of 6 students: 85, 92, 78, 95, 88, 91 # Calculate the mean, median, and standard deviation # YOUR CODE HERE: # ============================================================================ # PART 2: DATA FRAMES & DATA EXPLORATION # ============================================================================ # Instead of the built-in dataframes in R we will use tibbles install.packages("tibble") library("tibble") # Creating a data frame student_data <- tibble( name = c("Alice", "Bob", "Charlie", "Diana", "Eve"), age = c(22, 23, 25, 21, 24), grade = c(85, 92, 78, 95, 88), hours_studied = c(10, 15, 8, 18, 12) ) # Alternatively student_data <- tribble( ~name , ~age , ~grade , ~hours_studied , "Alice" , 22 , 85 , 10 , "Bob" , 23 , 92 , 15 , "Charlie" , 25 , 78 , 8 , "Diana" , 21 , 95 , 18 , "Eve" , 24 , 88 , 12 ) # Viewing data student_data View(student_data) # Opens in a separate viewer # Basic exploration head(student_data) # First 6 rows str(student_data) # Structure of the data summary(student_data) # Summary statistics dim(student_data) # Dimensions (rows, columns) names(student_data) # Column names # Accessing columns student_data$age student_data$grade mean(student_data$grade) max(student_data$hours_studied) # Loading built-in datasets data(mtcars) head(mtcars) ?mtcars # Help file # EXERCISE 2: # Using the mtcars dataset: # 1. How many rows and columns does it have? # 2. What is the mean miles per gallon (mpg)? # 3. What is the maximum horsepower (hp)? # YOUR CODE HERE: # ============================================================================ # KEY TAKEAWAYS # ============================================================================ # - R uses <- for assignment # - Vectors are created with c() # - Data frames are like Excel spreadsheets # - Access columns with $ # - summary() gives detailed regression output