Create and store objects in R using the assignment operator.
Perform basic arithmetic and use simple built-in functions.
Create, inspect, and modify a data frame.
Access values in a data frame using $ and indexing.
Create new variables and apply basic transformations in R.
In this first tutorial, we begin with the core building blocks of R. We will explore data structures in more detail in Chapter 2.
# --------------------------------------------------# Creating objects and basic arithmetic# --------------------------------------------------# Store values using the assignment operator <-x <-10y <-5# Perform basic calculationsx + y
[1] 15
x * y
[1] 50
# --------------------------------------------------# Creating and exploring a data frame# --------------------------------------------------# Create a simple data frame of citiescities <-data.frame(name =c("Washington DC", "Paris", "Seoul"),population =c(693645, 2048472, 9600000))# View the data framecities
name population
1 Washington DC 693645
2 Paris 2048472
3 Seoul 9600000
# Check column namescolnames(cities)
[1] "name" "population"
# --------------------------------------------------# Accessing data# --------------------------------------------------# Access a column using $cities$population
[1] 693645 2048472 9600000
# Access rows and columns using indexing [row, column]cities[1, ] # First row
name population
1 Washington DC 693645
cities[, "name"] # Name column
[1] "Washington DC" "Paris" "Seoul"
# --------------------------------------------------# Using functions# --------------------------------------------------# Calculate summary statisticsmean(cities$population)
[1] 4114039
sd(cities$population)
[1] 4799033
# --------------------------------------------------# Creating new variables# --------------------------------------------------# Add a new columncities$double_population <- cities$population *2# View updated data framecities
name population double_population
1 Washington DC 693645 1387290
2 Paris 2048472 4096944
3 Seoul 9600000 19200000
Your first code example
As a warm-up, we begin with a cowsay package to run our initial codes.
Note
The cowsay package displays messages using ASCII art animals. This makes console output more fun and engaging.
Install the package from CRAN onto your computer and load the package (once you install a package, you do not need to re-install it again. Next time, just use library() without install.packages().
Tip: If you type ? followed by a function name, the Help window will open on the right and explain what the function does
?say
Note
Try different animals! If you enter an animal name that is not supported, R will return an error such as: Error: unknown animal. The message will also display a list of supported animals; for example frog, ghost, pumpkin, dragon, bat, owl, duck, yoda, alligator.
When writing code in your R Markdown, make sure that every R code block starts with three backticks and {r}, and also ends with three backticks. The backtick key is usually located next to the 1 key on your keyboard (the same key as ~).