R Coding Basics

September 22, 2024

R Programming Basics

What Can R Do?


  • R is a powerful language for data analysis and visualization
  • It is also a general-purpose programming language
  • Does everything from web development to machine learning
  • It is open-source and has a large community of users and developers

R as a Calculator


  • R can be used as a simple calculator
  • You can perform arithmetic operations on numbers
# Addi a number and store it to a value
sum_of_2plus2 <- 2 + 2


sum_of_2plus2
[1] 4

Some Common Arithmetic Operators


  • + addition
  • - subtraction
  • * multiplication
  • / division
  • ^ exponentiation (also **)

Functions

  • A function is a set of instructions that produces some output
  • In R, you can use built-in functions to perform specific tasks
  • For example, you can use the mean() function to calculate the average of a set of numbers
  • To do this you have to use the combine function c() to create a vector of numbers


Create a vector of numbers and take the mean…


# Create a vector of numbers
numbers <- c(1, 2, 3, 4, 5)

# Calculate the mean of the numbers
mean(numbers)
[1] 3

Some Common Base R Functions

  • mean() calculates the mean of a set of numbers
  • median() calculates the median of a set of numbers
  • sd() calculates the standard deviation of a set of numbers
  • sum() calculates the sum of a set of numbers
  • length() calculates the length of a vector
  • max() and min() calculate the maximum and minimum values of a vector
  • round() rounds a number to a specified number of decimal places
  • sqrt() calculates the square root of a number
  • log() calculates the natural logarithm of a number
  • exp() calculates the exponential of a number
  • abs() calculates the absolute value of a number

Packages

From Functions to Packages


  • A function is a set of instructions
    • read_csv() is a function
    • ggplot() is a function
  • A package is a collection of functions
    • readr is a package that contains the read_csv() function
    • ggplot2 is a package that contains the ggplot() function
  • Use install.packages() to install packages
  • Use library() to load packages
  • You can install packages from CRAN

The Tidyverse

  • The Tidyverse is a collection of data science packages
  • It is also considered a dialect of R
  • In this class, we will be using many Tidyverse packages
    • ggplot2 for data visualization
    • readr for reading data
    • dplyr for data manipulation
    • tidyr for data tidying
    • Etc.
  • At first we will load the packages independently, e.g. library(ggplot2)
  • Later we will load them all at once with library(tidyverse)

Objects

What is an Object?

  • An object in R is a data structure used to store data
  • It can vary from simple scalar types to more complex data structures like vectors, lists, or data frames
  • Objects hold not only data but also information about the type of data and the operations that can be performed on them
  • Every entity in R is considered an object, making R a language based around the manipulation of objects

How to Store Data

  • In R, you can store data in objects using the assignment operator <-
  • The object name is on the left of <-, and the data or value you wish to assign to the object is on the right
  • Then you can print the object to the console using the object name
# Store the value 42 in the object my_number
my_number <- 42

# Print the value of my_number
my_number 
[1] 42

When to Store Data in Objects


  • Note that you don’t always have to store data in objects
  • You should mostly store data in objects when you want to use the data later
  • If you only need to use the data once, you can just use the data directly
# Add two numbers without storing them in an object
2 + 2
[1] 4

Your Turn!


  • Go to the Week 2 module on Posit Cloud
  • Work through the first set of challenges there
05:00

Three Examples

Let’s get going . . .


Let’s open up the Getting Started module on Posit Cloud…

And work through the examples there.

Example: Make a map!

Example: Make a map!


library(leaflet)
leaflet() %>% 
  addTiles() %>%   # Add default OpenStreetMap map tiles
  addMarkers(lat = 38.90243843683386, lng =  -77.0443814477152, 
             label = "Elliott School of International Affairs")
05:00

Example: Plotting Democracy Over Time

Example: Plotting Democracy Over Time

# Load the packages
library(vdemlite)
library(ggplot2)

# Use vdemlite to extract democracy scores for France and INdia
dem_data <- fetchdem(indicators = "v2x_polyarchy",
                     countries = c("FRA", "IND"))

# And now we can plot the data
ggplot(dem_data, aes(y = v2x_polyarchy, x = year, color=country_name)) +
  geom_line() +
  theme_minimal() +
  xlab("Year") +
  ylab("Electoral Democracy Index") +
  ggtitle("Electoral Democracy, 1970-2022") +
  geom_hline(yintercept = .5, linetype = "dashed", color = "grey") +
   scale_color_manual(name="Country", values=c("#E69F00", "#56B4E9")) +
  ylim(0, 1)
05:00