Official Site for R and Comprehensive R Archive Network (CRAN)
R for Data Science, 2nd Edition
R Programming for Statistics and Data Science
Open-source
Free
Platform independent
Reproducible
Shareable
Contains add-on packages
Created for data statistical computation and graphic export
File > New File > R Script
File > New File > R Markdown File > New File > Quarto Document
RStudio is an Integrated Development Environment (IDE) that allows you to save you code, store your variables and environments and view outputs.
this pane is opened when you create or open a markdown or script file.
This is where you interact with the R. The results of your commands are displayed in this pane.
view functions, objects, and data sets that are stored here
view files, plots, packages, and get help
Tools > Global Options
Some suggested Preferences to set:
Code > Editing > Use Native Pipe Operator
Code > Editing > Soft wrap source R files
The working directory in R is the folder where you are working. Hence, it’s the place (the environment) where you have to store your files of your project in order to load them or where your R objects will be saved.
Session > Set Working Directory > Choose Directory
Tools > Keyboard Shortcuts Help
| PC | MAC | |
|---|---|---|
| Run Code | CTRL + ENTER | CMD + RETURN |
| Assignment Operator | ALT + - | OPTION + - |
| Pipe Operator | CTRL + SHIFT + M | CMD + SHIFT + M |
Comments are used to provide context, documentation, and explanations for the code.
Commands your give to perform a task.
The information that you give to a function to tell it what to do.
Provides the necessary information, explanations, examples, and guidance to help you learn, understand, and effectively use R functions and packages.
Allows you to store and work on data (numbers, words, tables, and more).
The assignment operator (<-) allows you to create an object.
| PC | MAC | |
|---|---|---|
| Assignment Operator | ALT + - | CMD + - |
| Double or Numeric | used for numbers which can be integers (whole numbers) or real numbers (numbers with decimal points). |
| Character | used for text, words, and strings of characters. Enclose in double (““) or single (’’) quotes. |
| Factor | used to represent categorical data with predefined levels. |
| Date | used for handling dates, times, and time intervals. |
| Boolean | used for decsion-making and represented by binary values, typically TRUE or FALSE |
R packages are like toolkits or collections of pre-built functions, data sets, and tools that extend the capabilities of the R programming language.
You must install a package before you can load it. But you only need to install it one time.
For every new session, you must load it to use the package’s functions.
ordered collections of data items of the same type.
two-dimensional sequence of data variables (columns) and observations (rows). While each variable in a data frame typically contains data of the same type, different variable can contain different data types.
# create vectors
title <- c("Star Wars", "The Empire Strikes Back", "Return of the Jedi")
year <- c(1977, 1980, 1983)
length.min <- c(121, 124, 133)
box.office.mil <- c(787, 534, 572)
# combine these vectors with the data.frame() function
starWars.data <- data.frame(title, year, length.min, box.office.mil)
starWars.dataallows you to select and work with specific variables (columns) from a data frame.
Once you are done entering your data, you can export it to your working directory. The function without built-in arguments is write.table( ) but if are saving it as a csv, you are better using write.csv( ).
load data from a file in your working directory using the read_csv() function from tidyverse. There is also the read.csv( ) function in base R.