AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

R data types: Overview

PARTAGEZ

Like in all other programming languages, in R programming there are also different data types that help programmers to structure their data. In addition to the basic data types in R, this language also contains more complex data types.

Why use R data types?

Programming often involves processing data. To ensure that this data can be adequately stored in memory, developers have several data types to choose from in most languages. Data types can for example be numbers or character strings.

Typically, different R operators are defined on various data types. This is why data types also help programmers to process their data appropriately and thus increase the effectiveness of programs.

The different R data types

In R language, there are a number of data types. Unless you are new to programming, some R data types should help you recall other programming languages. To check data types in R, use the R command class()which receives any variable as a transfer parameter and returns the data type of that variable.

Numeric Data Types

Numeric data types are among the basic data types in R and are used to represent numerical values. Types of digital data are subdivided into several categories: numeric for real numbers, integer for integers and complex for complex numbers with an imaginary part.

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

R

The result of this code is as follows:

"numeric"
"numeric"
"complex"

R

At this point, you may be wondering why the data type numeric appears twice, although the value of the variabley either an integer or an integer. The reason is simple: in R, integers are always simultaneously numeric. To inform the interpreter that it is in fact an integer, you must append the suffixL to your integers:

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

R

Now, calling the above function returns the expected result:

Character strings

To represent text or characters, you can use the data type character. In R language, you can report data of this type, mentioned in R strings, with single and double quotes :

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

R

Here too, the “class()” function can be used to display the data type of your variables:

Logical Data Types

Variables of the logical data type, known in R language as logical, are parsed by the interpreter as “TRUE” or “FALSE”. In this way, the logical conditions or expressions can be formalized. This functionality is often necessary to command the control flow of the program.

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

R

If you look up the data type of the two variables, you find that the data type in R logical is assigned to them:

Raw data

In R language, there is a separate data type to see your variables as a sequence of bytes. This type of data in R is called raw. To convert your data into raw data, you can use the “charToRaw()” function. To do it in reverse, use the “rawToChar()” function.

The following code shows how a character string is converted into a series of bytes of type raw :

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

R

The code above then returns a sequence of bytes in hexadecimal numbers to the screen. Calling “class()” then ensures that the data type of the variable y is displayed:

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

R

In R language, you can also convert the data to another data type. Such a change of data types in R is frequent and even very simple: for a character string composed of a number (for example « 42 »), you can perform a simple addition with 0 and thus converting the character string to the data type numeric.

Data structures in R

In addition to the basic data types in R, there are a variety of data structures that help programmers better structure their data. These Data structures are more complex than simple data types and, unlike data types, are often also multidimensional, e.g. R dataframes.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact