You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This reference sheet contains the key objects that we use in DSCI 100, and a
brief syntax example for each of the main packages. During the closed book
exams, you will still have access to this page, so get familiar with it already
now. There is no guarantee that every function or parameter in the textbook is
covered here, but if you think there is something missing, please let us know
and we can consider adding it.
Note that we have only described use cases relevant to DSCI 100.
Sometimes we have included the exact parameter name of a function,
e.g. print(x),
other times we have opted to included a more descriptive name,
e.g. mean(column).
Base R Operations
Function
Description
abs(x)
Convert numeric value(s) to absolute value
as.data.frame(x)
Converts an object to a data frame
as.numeric(x)
Converts a variable to a numeric data type
c(1,2,3)
Combines values into a vector or list in R
is.na(column)
Detect missing (NA) values in a vector or data frame
dim(column)
Returns dimensions (rows and columns) of an R object
max(column)
Returns maximum value in a numeric vector
mean(column)
Returns average value in a numeric vector
median(column)
Returns the median value in a numeric vector
min(column)
Returns minimum value in a numeric vector
n()
Counts the number of rows in a table's group
names(tbl)
Assigns or retrieves names of elements in an R object
ncol(tbl)
Returns the number of columns in a matrix/data frame
nrow(tbl)
Returns the number of rows in a matrix/data frame
print(x)
Displays specified object's value
round(num, digits)
Rounds a number to specified decimals
sd(column)
Calculates standard deviation for numeric data
seq(from, to, by)
Generates a sequence of numbers
sum(column)
Calculates the sum of numeric values in a vector or matrix
sort(df)
Sorts a vector or data frame in ascending order
sqrt(num)
Computes the square root of a numeric value
Operators
Function
Description
==
Compares two values and returns TRUE if they are equal
%in%
Checks if elements on the left side are in the right
!
Negates a logical value (!TRUE is FALSE)
&
Performs element-wise logical AND operations
|
Represents the OR logical operator
|>
Pipe operator, which passes data from left to right
Data Reading
Function
Description
download.file(url, destfile)
Download a file from the web
read_csv(filepath)
Reads comma-separated values into a data frame
read_csv2(filepath)
Reads CSV files with semicolon delimiter
read_delim(filepath, delim, skip, col_names)
Reads data from a delimited text file
read_excel(filepath)
Reads Excel files into R data frames
read_html(filepath)
Reads and parses HTML web pages
read_tsv(filepath)
Reads tab-separated values into a data frame
write_csv(tbl, filepath)
Writes data to a CSV file
Database functions:
Function
Description
collect(database_table)
Convert a database table to a tibble
dbConnect(database, dbname)
Establishes a connection to a database
dbListTables(dbConnect_object)
Lists tables in a database connection
RPostgres::Postgres()
Connects to and interacts with PostgreSQL databases
RSQLite::SQLite()
Access and manage SQLite database connections
tbl(dbConnect_object, table_name)
Creates a data frame from a data source
Data Wrangling
Function
Description
across(column_range, function)
Apply the given function to each column in the specified column range
arrange(tibble, columns_as_arguments)
Order rows by the values of the given columns (default is increasing)
colnames(tbl)
Get a list of column names from a tibble
desc(column)
Sort a column (or numeric vector) in descending order