R Basics
February 8, 2024
tidyverse
Package
#
mark is R’s comment character.
*.R
files) and in a code chunk in Quarto (*.qmd
), #
indicates that the rest of the line is to be ignored.#
(commenting).Home/End moves the blinking cursor bar to the beginning/End of the line.
Ctrl (command for Mac Users) + Z undoes the previous action.
Ctrl (command for Mac Users) + Shift + Z redoes when undo is executed.
Ctrl (command for Mac Users) + F is useful when finding a phrase (and replace the phrase) in the RScript.
libr
in the RScript in RStudio and wait for a second.
A value is datum (literal) such as a number or text.
There are different types of values:
What is going on here?
The shortcut for the assignment <-
is:
y <- x + 12
, it does the following:
<-
in the middle.x
and adds it to 12
).y
.Sometimes you will hear variables referred to as objects.
Everything that is not a literal value, such as 10
, is an object.
TRUE
or FALSE
.
class()
function returns the data type of an object.character
in R."
or single quotes '
to wrap around the stringfavorite.integer <- as.integer(2)
class(favorite.integer)
favorite.numeric <- as.numeric(8.8)
class(favorite.numeric)
class(TRUE)
class(FALSE)
favorite.numeric == 8.8
favorite.numeric == 9.9
class(favorite.numeric == 8.8)
==
to test for equality in Ra <- 1:10 # colon operator
b <- c("3", 4, 5)
beers <- c("BUD LIGHT", "BUSCH LIGHT", "COORS LIGHT",
"MILLER LITE", "NATURAL LIGHT")
class(a)
class(b)
class(beers)
We can create one-dimensional data structures called “vectors”.
c(...)
: Returns a vector that is constructed from one or more arguments, with the order of the vector elements corresponding to the order of the arguments.
Factors store categorical data.
Under the hood, factors are actually integers that have a string label attached to each unique integer.
Male
/Female
labels for each of our patients, this will be stored a “column” of zeros and ones by R.+
+
:+
tells you that R is waiting for more input; it doesn’t think you’re done yet.A function can take any number and type of input parameters and return any number and type of output results.
R ships a vast number of built-in functions.
R also allows a user to define a new function.
We will mostly use built-in functions.
library(tidyverse)
# The function `str_c()`, provided by `tidyverse`, concatenates characters.
str_c("Data", "Analytics")
str_c("Data", "Analytics", sep = "!")
We invoke a function by entering its name and a pair of opening and closing parentheses.
Much as a cooking recipe can accept ingredients, a function invocation can accept inputs called arguments.
We pass arguments sequentially inside the parentheses (, separated by commas).
A parameter is a name given to an expected function argument.
A default argument is a fallback value that R passes to a parameter if the function invocation does not explicitly provide one.
All of the basic operators with parentheses we see in mathematics are available to use.
R can be used for a wide range of mathematical calculations.
sum()
calculates the sum of all numbers in a vector.mean()
calculates the arithmetic mean of the values in a vector. \[
\overline{x} = \frac{x_{1} + x_{2} + \cdots + x_{N}}{N}
\]sd()
calculates the standard deviation (SD) of the values in a vector.
NULL
and NA
valuesNULL
is just an alias for c()
, the empty vector.NA
indicates missing or unavailable data.NULL
and NA
valuesis.na()
is to check whether expression evaluates to NA
."A" == NA
evaluate to NA
?Sometimes we need to explicitly cast a value from one type to another.
as.character()
, as.integer()
, as.numeric()
, and as.factor()
.NA
.Complete path from the root directory to the target file or directory.
Independent of the current working directory.
Example (Mac): /Users/user/documents/car_data.csv
Example (Windows): C:\\Users\\user\\Documents\\car_data.csv
/Users/user
, the relative path to car_data.csv
would be documents/car_data.csv
read_csv()
function to read a comma-separated values (CSV) file.Download the CSV file, car_data.csv
from the Class Files module in our Brightspace.
Find the path name for the file, car_data.csv
from the File Explorer / Finder.
Provide the path name for the file, car_data.csv
, to the read_csv()
function.
View()
/view()
displays the data in a simple spreadsheet-like grid viewer.
dim()
shows how many rows and columns are in the data for data.frame
.nrow()
and ncol()
shows the number of rows and columns for data.frame
respectively.skimr::skim()
provides a more detailed summary.
skimr
is the R package that provides the function skim()
.data.frame
: Variables, Observations, and Values
There are three rules which make a data.frame
tidy: