Comprehensive Study Notes on R Programming Fundamentals: Shortcuts, Syntax, Data Types, and Structures
RStudio Workspace Shortcuts & Basic Operations
Key RStudio keyboard shortcuts for efficiency in the script panel and console:
Command + Enter(orControl + Enteron Windows): Runs the current line of code or the entire highlighted script block from the script editor panel into the console.Command + S(orControl + Son Windows): Saves the active script file.Command + L: Clears all text and output from the interactive console window.
Performing basic mathematical calculations in R:
Division:
401 / 4(evaluates to ).Multiplication:
5 * 4(evaluates to ).Exponentiation:
2^3(evaluates to ).
Variables, Assignment, and Coding Style
Distinction between assignment and equality checking:
The double equal sign
==is reserved exclusively for relational equality checking, not for value assignment.The left-facing arrow
<-(constructed using the less-than symbol<and a hyphen-) is the standard operator used to assign values to variables.
Mechanics of variable assignment:
Assigning a numeric value to a variable:
x <- 10adds the variablexwith a value of to the Environment panel.Assigning another variable:
y <- 2stores insidey.Reassigning variables using existing variables: Executing
y <- xupdatesyso that bothxandystore the value .
Recommended R coding standards:
Although the single equals sign
=can function as an assignment operator in R, its use is strongly discouraged in favor of<-.Proper spacing rule: Always insert exactly one space before and after the assignment operator (e.g.,
z <- 3).Formatting justification: Leaving spaces around operators makes scripts clean, legible, and maintainable, preventing visual overcrowding in large codebases exceeding 100 or 1,000 lines.
Basic R Data Types
R features four primary foundational data types:
Numeric: Represents numbers (e.g., , , , , ).
Character: Represents text strings. Text must be enclosed inside double quotation marks
"..."or single quotation marks'...'(e.g.,name <- "Nancy").Logical: Represents boolean values and must always be typed in full uppercase without quotation marks:
TRUEorFALSE(e.g.,good <- TRUE).Factor: Represents categorical variables with predefined categories known as levels.
RStudio Auto-Completion Feature:
Typing partial variable or function names (e.g., typing
GOOwhen a variablegoodexists) triggers RStudio's auto-complete pop-up helper.Hitting
Enterautomatically completes long variable or function names, reducing typing errors and saving time during script development.
Help Documentation & Diagnostic Functions
Accessing internal R help menus:
Click the Help tab directly in the RStudio user interface.
Type a single question mark
?followed immediately by the function name (with no space between) directly into the console (e.g.,?factor). Ensure the input layout is set to English to avoid non-standard question mark symbols.Pressing
Enterdisplays the function's description, formal syntax structure, required parameters, and usage details.
Type checking functions:
is.character(variable): Evaluates whether a target variable is of character type (e.g.,is.character(x)returnsFALSEwhenxis numeric).is.logical(variable): Evaluates whether a target variable is of logical type (e.g.,is.logical(g)returnsFALSEwhengis a vector or factor).
Error handling mindset:
Console error messages and red error text serve as critical feedback tools rather than failures.
Errors identify precise line locations, parameter mismatches, and structural problems necessary for debugging.
Vectors and Factors
Vector Creation with
c():Multiple elements of the same basic data type must be grouped into a single multi-element data structure called a vector.
Vectors are instantiated using the combine function
c()with comma-separated values.Example:
g <- c("m", "f")combines individual character strings"m"and"f"into a single character vectorg.
Factor Creation with
factor():Factors store categorical values and automatically track unique category labels called levels.
Passing raw separated strings directly to
factor()(e.g.,factor("m", "f")) throws an error because the function interprets the second string as an unintended argument rather than part of the input dataset.Correct multi-step factor assignment: Create a vector
g <- c("m", "f"), then convert it viagender <- factor(g).Correct single-line factor assignment:
gender <- factor(c("m", "f"))directly wraps the vector combine function inside the factor function.Inspecting a factor variable in the console (e.g., typing
gender) prints the stored values along with its definedLevels(e.g.,Levels: f m).
Advanced Data Structures: Lists, Matrices, and Data Frames
Overview of complex data structures:
List: Heterogeneous collection capable of storing multiple data types simultaneously (numeric, character, logical, vectors, factors, or nested lists) inside a single container object.
Matrix: Multi-dimensional collection containing elements of a single uniform data type.
Data Frame (
DataFrame): Two-dimensional, tabular data structure composed of rows and columns, where each column can hold a different data type.
Constructing and Inspecting Lists:
Constructed using the
list()function.Simple single-element list:
l <- list(x)wraps variablexinto a list object.Multi-element heterogeneous list:
l <- list(x, name)combines numericx() and charactername("Nancy").Viewing list objects: Clicking a list in the Environment panel reveals an expanded hierarchy listing every contained index, its assigned data type, and its stored value.
Environment Organization: Complex structures like Lists and Data Frames display at the top of the Environment panel accompanied by an expandable blue icon because they store complex, high-dimensional dataset collections.