Similar to other pro­gram­ming languages, when pro­gram­ming 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?

Pro­gram­ming often involves the pro­cessing of data. To fa­cil­it­ate the storing of data in a logical way, most languages offer de­velopers a variety of data types to choose from. Numbers and char­ac­ters are just two examples of data types that a pro­gram­mer can use to organise data.

Different data types have specific op­er­a­tions that can be performed on them. As such, different R operators are typically designed to work with specific data types. This helps pro­gram­mers to process data ef­fect­ively, which, in turn increases the ef­fi­ciency 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 recognise 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 brackets 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 spe­cific­ally, a whole number. This is because integers are always con­sidered numeric in R. In order to let the in­ter­pret­er 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 char­ac­ters, you can use the data type character. Data that cor­res­ponds 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 in­ter­pret­er as being either TRUE or FALSE. This allows for con­di­tions or logical ex­pres­sions to be form­al­ised, 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 hexa­decim­al 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 rep­res­ents 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 struc­tures 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 struc­tures that can help pro­gram­mers to better organise their data. These data struc­tures are more complex than the simple data types. Unlike other data types in R, more complex data types like R data frames are often mul­ti­di­men­sion­al.

Tip

In­ter­ested in coding with R? Get your R project online with web hosting from IONOS.

Go to Main Menu