R is a programming language used for data analysis, statistics, graphing, and scientific computing. This cheat sheet helps students remember the most common commands and patterns used in beginner R programs. It is useful when writing scripts, cleaning data, making plots, or checking calculations.
The focus is on quick syntax reminders rather than long explanations.
Key Facts
- Assign a value to a variable with x <- 10, and print it by typing x or using print(x).
- Create a vector with c(), such as scores <- c(82, 91, 77, 88).
- Access vector items with square brackets, where scores[1] returns the first item because R uses 1-based indexing.
- Create a data frame with data.frame(name = c("Ana", "Ben"), score = c(90, 85)).
- Select a data frame column with df$score or df[ , "score"].
- Define a function with add <- function(a, b) { return(a + b) }.
- Calculate common summaries with mean(x), median(x), sum(x), min(x), max(x), and sd(x).
- Make basic plots with plot(x, y), hist(x), boxplot(x), and barplot(values).
Vocabulary
- Variable
- A named storage location that holds a value, such as x <- 5.
- Vector
- An ordered collection of values of the same basic type, created with c().
- Data frame
- A table-like structure in R with rows and columns, often used to store datasets.
- Function
- A reusable block of code that takes input values, performs steps, and often returns an output.
- Index
- A position number used to access an item in a vector, list, or data frame.
- Package
- A collection of extra R functions and data that can be installed and loaded when needed.
Common Mistakes to Avoid
- Using = when the class expects <- for assignment can make code harder to read or inconsistent with R style, even though = sometimes works.
- Forgetting that R starts indexing at 1 is wrong because x[0] does not return the first item in a vector.
- Mixing text and numbers in one vector changes the data type, so c(1, 2, "3") stores values as text rather than pure numbers.
- Writing a column name without the data frame name can fail because R may not know which table the column belongs to.
- Forgetting parentheses in a function call is wrong because mean is the function object, while mean(x) actually runs the calculation.
Practice Questions
- 1 Create a vector named temps with the values 68, 72, 75, 71, and 69, then write the R command to find its mean.
- 2 Given scores <- c(84, 91, 78, 88), what value is returned by scores[2]?
- 3 Write an R command that creates a data frame named students with columns name = c("Mia", "Leo") and grade = c(95, 89).
- 4 Why is a data frame usually better than separate vectors for storing a class roster with names, ages, and test scores?
Understanding R Language Quick Reference
R works best when you treat data as objects with a type and a shape. A single number is different from a vector, and a vector is different from a table. This matters because many R commands act on every value in a vector at once.
If a class has thirty test scores, you can find their average without writing a loop for each student. This vectorized style is one reason R is useful for real datasets. Pay attention to whether values are numbers, text, logical values, or dates.
Text labels such as student names cannot be averaged. Logical values are true or false, and they are useful for selecting rows that meet a condition.
Missing data needs careful handling. R often represents an unknown or unavailable value as NA. A single NA can cause a summary such as a mean to return NA, because R cannot safely guess the missing value.
Many summary functions have an option that removes missing values before calculation. Removing values may be sensible, but it can change the result if many records are missing. Students should check how much data is absent and consider why it is absent.
A blank survey answer, a broken sensor, and a student who was absent can mean different things. Good analysis starts with checking the data rather than trusting every output.
Data frames resemble a spreadsheet, but each column should describe one variable. For example, one column might hold height, another might hold shoe size, and another might hold a group label. Each row should usually represent one observation, such as one person or one experiment trial.
This structure makes it easier to sort, filter, compare groups, and calculate summaries. When selecting part of a data frame, be clear about whether you want rows, columns, or individual cells. A common error is mixing up a column name with a position number.
Another is assuming the first row is numbered zero. R starts counting at one, which can produce off by one mistakes when students have learned languages that begin at zero.
Graphs are not just decoration after a calculation. They help reveal patterns that one average can hide. A histogram shows the spread and shape of one numerical variable.
A box plot helps compare groups and spot unusually high or low values. A scatter plot can show whether two measurements tend to change together, such as study time and score. A visible trend does not prove that one variable caused the other.
Sleep, prior knowledge, test difficulty, and many other factors may matter. Label axes clearly, include units, and choose a graph that fits the data. Functions help keep repeated work consistent.
A well named function can clean a column, calculate a result, or make the same graph for several groups. Test functions with simple known values before using them on a full dataset.