# 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:

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!

## 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!

## Related posts

### 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 –

### R Beginner Tutorial – Matrices

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

### R Beginner Tutorial – Lists

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

### R Beginner Tutorial – Vectors

R Academy Menu ggplot2 basics: learn ggplot2 in 15 minutes! R Beginner Tutorial R Beginner Tutorial – Basic Syntax R Beginner Tutorial – R Objects

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