# Addition
2 + 2
# Subtraction
5.432 - 34234
# Multiplication
33 * 42
# Division
3 / 42
# Modulo (Remainder)
2 %% 2
# Power
2^2
# Combine operations
2 + 2) * 5)^(10 %% 10) ((
Day 1 - Introduction to Data Analysis with R
Freie Universität Berlin - Theoretical Ecology
March 14, 2025
radius <- 5
There are 6 so-called atomic data types in R. The 4 most important are:
Numeric: There are two numeric data types:
Double: can be specified in decimal (1.243
or -0.2134
), scientific notation (2.32e4
) or hexadecimal (0xd3f1
)
Integer: numbers that are not represented by fraction. Must be followed by an L
(1L
, 2038459L
, -5L
)
Logical: only two possible values TRUE
and FALSE
(abbreviation: T
or F
- but better use non-abbreviated form)
Character: also called string. Sequence of characters surrounded by quotes ("hello"
, "sample_1"
)
Vectors are data structures that are built on top of atomic data types.
Imagine a vector as a collection of values that are all of the same data type.
Use the function c()
to combine values into a vector
There are many more options to create vectors
seq()
to create a sequence of numbers:
creates a sequence of numbers with an increment of 1 (e.g. 1:10
)rep()
ro repeat valuesLet’s create some vectors to work with.
# list of 10 biggest cities in Europe
cities <- c("Istanbul", "Moscow", "London", "Saint Petersburg", "Berlin",
"Madrid", "Kyiv", "Rome", "Bucharest", "Paris")
population <- c(15.1e6, 12.5e6, 9e6, 5.4e6, 3.8e6, 3.2e6, 3e6, 2.8e6, 2.2e6, 2.1e6)
area_km2 <- c(2576, 2561, 1572, 1439,891,604, 839, 1285, 228, 105 )
Divide population and area vector to calculate population density in each city:
The operation is performed separately for each element of the two vectors and the result is a vector.
Same, if a vector is divided by vector of length 1 (i.e. a single number). Result is always a vector.
We can also work with relational and logical operators
The result is a vector containing TRUE
and FALSE
, depending on whether the city’s population is larger than the mean population or not.
Check whether elements occur in a vector:
The %in%
operator checks whether multiple elements occur in a vector.
%in%
always returns a vector of the same length as the vector on the left side
You can use square brackets []
to access specific elements from a vector.
The basic structure is:
vector [ vector of indexes to select ]
Change the values of a vector at specified indexes using the assignment operator <-
Imagine for example, that the population of
You can also index a vector using logical tests. The basic structure is:
vector [ logical vector of same length ]
Introduction to R
# By index
v[3]
v[1:4]
v[c(1,5,7)]
# Logical indexing with 1 vector
v[v > 5]
v[v != "bird" | v == "rabbit"]
v[v %in% c(1,2,3)] # same as v[v == 1 | v == 2 | v == 3]
# Logical indexing with two vectors of same length
v[y == "bird"] # return the value in v for which index y == "bird"
v[y == max(y)] # return the value in v for which y is the maximum of y
Task (30 min)
Working with vectors
Find the task description here
Selina Baldauf // Introduction R
Comments in R
#
is a commentCtrl/Cmd + Shift + R
)