Excel Unplugged

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.

Data types of vectors

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

  1. Numeric vectors, for example 7.5, 12, 0.4, -2
  2. Integer vectors, for example 1L, 12L, 0L, as.integer(12), as.integer(12.1) (L stands case stands for »long integer«)
  3. Complex vectors, for example 2 + 5i, -2i, 4 – 3i
  4. Character vectors, for example, “I love R!”, “my string”, “9”, ‘0.9’
  5. 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 structures

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:

HomogeneousHeterogeneous
1dAtomic vectorList
2dMatrixData frame
ndArray
R objects

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

R objects and data types: vector, matrix, array, data frame and list
1 Data structures in R. Source: Ceballos and Cardiel 2013.

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!

In a hurry?

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

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

References

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

Objects Objects

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.