### R Beginner Tutorial – Arrays

R Academy Menu ggplot2 basics: learn ggplot2 in 15 minutes!R Beginner TutorialR Beginner Tutorial – Basic SyntaxR Beginner Tutorial – R ObjectsR Beginner Tutorial –

Home » R Beginner Tutorial – R Objects

What are different objects in R? R is an object-oriented language which means all variables refer to objects. The most basic object is a vector or a sequence of elements. Even a single element is treated as a vector of length one. Consequently, almost all other data structures are built on vectors.

Importantly, a vector object can only contain elements of the same type (for example only integers), while lists are more versatile and can contain elements of any type.

Subsequently, R knows 5 different types or classes of vectors: numeric, integer, complex, character and logical. Let’s look at some examples.

**Numeric vectors**, for example 7.5, 12, 0.4, -2**Integer vectors**, for example 1L, 12L, 0L, as.integer(12), as.integer(12.1) (L stands case stands for »long integer«)**Complex vectors**, for example 2 + 5i, -2i, 4 – 3i**Character vectors**, for example, “I love R!”, “my string”, “9”, ‘0.9’**Logical vectors**, for example TRUE, FALSE

To clarify, numeric, integer and complex type are dedicated to numbers, character type to strings and logical to boolean values.

We use **class()** function to check data type.

```
> class(7.5)
[1] "numeric"
> class(2)
[1] "numeric"
> class(as.integer(2))
[1] "integer"
> class(2L)
[1] "integer"
> class(2+3i)
[1] "complex"
> class("I love R!")
[1] "character"
```

Data type or mode usually refer to what is stored (integer, character, logical, etc). The term data structure refers to how the data is stored (vector, list, data fra, etc.)

Furthermore, data structures can be arranged by their dimension (1d, 2d or nd) and their content: they can either be homogeneous (meaning all elements are of same type) or heterogeneous (elements are of different types). As a result, this gives rise to the five data types most often used in data analysis:

Homogeneous | Heterogeneous | |

1d | Atomic vector | List |

2d | Matrix | Data frame |

nd | Array |

Subsequently, almost all other objects are built on these foundations.

As illustrated on the image above, vectors acts as building blocks for matrices, arrays, data frames and lists. Data frames can contain vectors of different types, while matrices can only contain vectors of same type. Similarly, lists can contain all sorts of other objects (even other lists), that differ in type.

In conclusion, these were the basics you need to understand R objects. Furthermore, we’ll look at each data structure and see how to work with them. Stay tuned!

Meanwhile, in a hurry? If you’re already running late, we recommend you head over to our two R crash courses:

- R Language overview, fast and easy beginner tutorial on R
- ggplot2 Basics, fast and easy beginner tutorial on plotting data in R

For even more amazing tips check out our awesome ExcelOlympics YouTube channel!

1 Ceballos, Maite and Nicolás Cardiel. 2013. “Data structure.” First Steps in R. Accessed 2021-10-12.

x

x