R Basics
Objects, data types, operators, and how R computes and shows results
Data types and objects
Data is information that a computer can store and work with. It comes in different types — the most common ones you will encounter in R are:
| Type | Example values | R type |
|---|---|---|
| Numbers | 74.2, 3.14 |
numeric / double |
| Whole numbers | 1, 6, 100 |
integer |
| Text | "PASS", "batch_01" |
character |
| True/False | TRUE, FALSE |
logical or boolean |
In R, decimal numbers use a . not a , — so three and a half is written 3.5, not 3,5.
The type matters because it determines what you can do with a value. You can calculate the average of numbers, but not of text.
In R, even a single value is stored as an object. When you have multiple values of the same type, they form a vector. And when you organize vectors into columns, you get a tibble — a modern version of a data frame with rows (observations) and columns (variables), similar to a table.
You create an object by assigning a value to a name using <-. On the left side of <- you write the object name. On the right side you write the value you want to store.
Naming rules:
- Start names with a letter
- Use only letters, numbers, and
_ - Do not use spaces or special characters like
-,/,?,! - Names are case-sensitive (
dose_mgandDose_mgare different) - Use descriptive names, ideally with units when relevant (for example
weight_kg)
Examples:
- Good:
dose_mg,patient_id,reaction_time_s - Avoid:
dose mg(space),2dose(starts with number),x(too vague)
Multiple values of the same type can be combined using the c() function to form a vector:
And when you organize vectors into columns, you get a tibble. Since a tibble is a function from the tidyverse, remember to load the library:
Use clear, consistent naming from the beginning. Prefer snake_case (lowercase with _) such as air_temperature and dose_mg.
Data is read into R from files using functions like read_csv().
Within RStudio, you can inspect your data objects in the Environment panel, or using
view(name_of_your_object)Comments
Lines starting with # are comments — R ignores them when running code. Use comments to explain what your code does:
Operations
An operator is a symbol that performs an action on one or more values. They cover assignment (<-), arithmetic (+, -, etc.), comparison (==, >, etc.), and the pipe (%>%).
Assignment operator
| Operator | Meaning | Example | Result |
|---|---|---|---|
<- |
Assign a value to a name | x <- 42 |
x now stores 42 |
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Addition | 3 + 2 |
5 |
- |
Subtraction | 10 - 4 |
6 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 20 / 4 |
5 |
^ |
Exponentiation | 2 ^ 3 |
8 |
%% |
Modulo (remainder) | 7 %% 3 |
1 |
Comparison operators
Used to test conditions — return TRUE or FALSE:
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 5 == 5 |
TRUE |
!= |
Not equal to | 5 != 3 |
TRUE |
> |
Greater than | 7 > 3 |
TRUE |
< |
Less than | 2 < 1 |
FALSE |
>= |
Greater or equal | 5 >= 5 |
TRUE |
<= |
Less or equal | 3 <= 2 |
FALSE |
Pipe Operator
The pipe takes the output of one step and passes it as the input to the next — read it as “and then”. This allows you to chain multiple operations into a readable sequence without creating intermediate objects.
| Operator | Meaning | Example | Result |
|---|---|---|---|
%>% |
Pass result to next function | data %>% filter(age > 18) |
Filtered data frame containing entries where age is larger than 18 |
Computation
In R, data and computation are kept separate. You first store your data in objects, and then write expressions that compute with those objects. This is different from working in Excel, where data and formulas are mixed together in the same cells. In R, your data stays fixed in its object — computation produces a new object, leaving the original unchanged.
However, it is possible to overwrite the content of objects if you assign a different value to an existing object.
You can add or transform columns in a tibble using mutate():
A common application case for computing is when you calculate new values and add them as new columns in an existing tibble using mutate(). Assume capsules is a tibble you loaded before containing the column fill_mass_mg. You now add two columns ibu_mg and pct_label which store newly computed values.
R is vectorised — operations apply to entire columns at once, with no need for loops.
Output
R produces output in three main ways: printing to the console, viewing objects, and plotting.
Print to Console
For a quick check, run a line that contains only an object name:
R will print the current value in the Console. This is useful when you just want to inspect a result quickly.
When you want clear labels and units in your output, use cat():
Always round at the point of printing — store intermediate values at full precision:
Use signif() for concentrations and masses (significant figures), and round() for percentages and fixed decimal places.
View Objects
Inspect a data object in the RStudio Environment panel.
For tibbles/data frames, start with glimpse() to quickly see column names, data types, and example values:
glimpse(your_data_object)If you want a spreadsheet-like view, open it as a table:
view(your_data_object)Plotting
Visualize data using ggplot2:
ggplot(your_data_object, aes(x = fill_mass_mg, y = pct_label)) +
geom_point()Plots appear in the Plots panel in RStudio.
Workflow
A typical R script follows this pattern:
- Load packages —
library() - Read data —
read_csv() - Compute —
mutate(),filter(),summarise(),mean(),sd() - Output —
cat(),view(),ggplot()
Browse the function reference to see all covered functions.
Before your first real practical, see Setting Up an R Project to organize your scripts and data so your work stays reproducible.