Data: Breakfast cereals
RStudio is an Integrated Development Environment (IDE) for R. You need to download BOTH R and RStudio for RStudio to work.
While you can write R code without RStudio or another IDE, the interface is not as nice.
As a temporary alternative: you can use Posit Cloud to run RStudio online without installing anything on your computer.
Start a new R script: File - New File - R Script. Do NOT choose “new project.”
Default layout of RStudio:
A few reminders:
Run code directly in the console:
(Recommended) Write code in R scripts:
To run code from an R script:
To do \(\sqrt{100}\) and \(e^2\):
To do \(\ln{(1000)}\) (log base e is the default; can change the base using log(1000, base=10))
Exercise: calculate the following in R
Use # to comment out text
Space and empty lines, in general, do not matter in R
R is case sensitive!
The ability to develop and use R packages is a big advantage of R over other commercial statistical software like SPSS or excel.
Before you can use a package, there are two steps:
install.packages()library() (recommended)The Lock5Data package contains dataset from Lock, Lock, Lock, Lock, and Lock textbook Statistics: Unlocking the Power of Data
The tidyverse is a collection of very popular and powerful packages for data manipulation, visualization, formatting, etc.
We will explore the Cereal dataset to get more familiar with R. The code will be explained in more detail later.
Create a health score of cereals with your own formula, and store it in the dataset as health.
Print out the names of the cereals from the dataset.
Print out information of cereals that are manufactured by Kellog.
Print out information of cereals with a fiber content greater than 3 (grams per cup).
Print out information of the top 5 healthy cereals according to the score health.
Generate a scatterplot between Calories and health. Briefly discuss any findings (does your health score make sense based on the calories?)
Examples of R object: a number, a vector, a data frame, a function, etc.
To assign a number to an R object called “a”:
Cereal$Name is a vector of 30 valuesRecall: R is case sensitive!
c( , ) for combined (concatenated) valuesvector1 ± vector2, R will do the arithmetic operation element by element
col = "red"TRUE (or T) and FALSE (or F) are special logical characters when NOT in quotation
Numerically, R treats TRUE as 1 and FALSE as 0
A variable with only two options, (e.g., yes/no, true/false), is referred to as a “binary,” “dichotomous,” “indicator,” or “dummy” variable
public and code it as TRUE (1) for public and FALSE (0) for privateExercise: create a vector, names, that contains the names of some students; create another vector, cats, that indicates if a student owns at least one cat or not
IMPORTANT: do not confuse == and =!
which( ) will return the INDEX of TRUE values
c( , )The function append is more general. Run ?append to get more information
Nothing is stored! Recall, to store, you have to assign the new vector to an R object
Subsetting is the process of extracting element(s) from a vector or a dataset. The basic idea is to specify the position(s) of the elements you want, using square brackets [].
Q: what will names[c(2,2,2,3)] return?
Can also subset using a logic condition: R returns elements corresponding to TRUE
which( ) function to tell you the index position firstUse the minus sign to delete the corresponding elements from a vector
Note: tidyverse has functions to do data modification and subsetting in a more readable way (e.g., filter(), select(), mutate(), arrange()). More on this later.