What are the most important R data types?

Similar to other programming languages, when programming with R, you also have a variety of data types that you can choose from to structure your data. In R, you have basic data types as well as more complex data types.

Why are there different data types in R?

Programming often involves the processing of data. To facilitate the storing of data in a logical way, most languages offer developers a variety of data types to choose from. Numbers and characters are just two examples of data types that a programmer can use to organize data.

Different data types have specific operations that can be performed on them. As such, different R operators are typically designed to work with specific data types. This helps programmers to process data effectively, which, in turn increases the efficiency of their programs.

An overview of different data types in R with code examples

In R there are several data types. If you have already learned how to code with other languages, you may recognize some of the data types in R. If you want to check the data type of a variable in R, you can use the R command class(). When you place a variable in the parentheses of this function, the class function outputs the data type of the variable.

Numeric data type

Numeric data types belong to the category of basic data types in R. These data types are used for numerical values. Within the numeric data types, there is the numeric data type, which is used for real numbers, integer, which is used for integers, and complex which refers to complex numbers that contain an imaginary component.

x <- 3.14
y <- 42
z <- 3 + 2i
print(class(x))
print(class(y))
print(class(z))
R

Here is the output from the code above:

"numeric"
"numeric"
"complex"

You may be wondering why the code output the data type numeric twice, even though the y variable is an integer, or more specifically, a whole number. This is because integers are always considered numeric in R. In order to let the interpreter know that the number is an integer, you need to add an L at the end of the whole number:

y <- 42L
print(class (y))
R

Now when the function is called, it will output integer instead of numeric:

"integer"

Character data type

For text and characters, you can use the data type character. Data that corresponds to this data type is specified in R Strings, which are set apart by single or double quotation marks:

x <- "Hallo Welt!"
y <- 'Hello world!'
print(class(x))
print(class(y))
R

You can also use the class() function here to identify the data type of the variables:

"character"
"character"

Logical data type

Variables belonging to the logical data type are evaluated by the interpreter as being either TRUE or FALSE. This allows for conditions or logical expressions to be formalized, which is often necessary to control the flow of execution in a program.

x <- TRUE
y <- FALSE
print(class(x))
print(class(y))
R

Using the class () function, you will see that both variables have been assigned the R data type logical:

"logical"
"logical"
R

Raw data type

In R, there is also a data type that allows you to view variables as a sequence of bytes. This R data type is known as raw. To convert your data into a raw format you use the charToRaw() function. To convert raw data back into its original format you can use the function rawToChar.

The following code shows how to convert a string into a sequence of bytes. The y variable belongs to the raw data type:

x <- "Hallo Welt!"
y <- charToRaw(x)
print(y)
print(class(y))
R

The code initially outputs a hexadecimal byte sequence. After the byte sequence, the class() function is called, which displays the data type of the y variable:

48 61 6c 6c 6f 20 57 65 6c 74 21
"raw"
R
Note

It’s also possible to change the data types of data in R. Most changes are simple to carry out. For example, if you have a string that represents a number (let’s say “42”), you can simply add 0 to convert it from the character data type to the numeric data type.

Which other data structures are there in R?

In addition to the basic data types in R that we covered in this article, there are also a variety of other data structures that can help programmers to better organize their data. These data structures are more complex than the simple data types. Unlike other data types in R, more complex data types like R data frames are often multidimensional.

Tip

Interested in coding with R? Get your R project online with web hosting from IONOS.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.