AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Quick overview of R Lists

PARTAGEZ

Data structured in the form of lists is present in different programming languages. Lists in R can be particularly useful for grouping R data of different types.

R Lists: what are they for?

Lists are multipurpose data structures. In R, they even constitutea data type. Lists are particularly suited to dynamic programming environments, where different types of data elements are added and removed flexibly. Unlike R vectors, the data type of list elements is irrelevant; the possibility ofassociate different types of data within the same list is also one of the main advantages of R Lists. Since you can nest them as you wish, R Lists allow you to represent complex situations in the source code.

Create lists in R: develop R Lists

“list()” function

To create a simple R List, simply use “list()”, the function of the same name, by entering the elements to add to your list.

liste <- list(1, TRUE, "Hello World!", c(1,2,3,4))

R

In the code example above, a list with four values was created. As you can see, the data type of these values ​​is not the same, but they can all end up in the same list.

R also allows you to create a list composed of named elements, in order to more easily access certain elements of your R List.

personne <-list(nom = "Paul Michel", age = 27, hobbies = c("Programmation, Cinema"))

R

It is also possible to pass lists to other lists (as part of them). For example, different lists can be grouped into a single data set:

personne <- list(list(nom = "Paul Michel", age = 27, hobbies = c("Programmation, Cinéma")), list(nom = "Mia Petit", age = 32, hobbies = c("Théatre, Jeux vidéo")), list(nom = "Sarah Henry", age =23, hobbies = c("Sport, Musique")))

R

Create lists from vectors

You can also choose to create lists from R vectors. To do this, you must give them the form of a list. In R, a function is also provided for this purpose.

vecteur <- c(1,2,3,4)
liste <- as.list(vecteur)

R

Convert lists to different data structures

R Lists are a versatile data structure, but you may need different structures for specific applications, including Vectors or DataFrames. In such cases, your lists can easily be converted.

R List in vector: create a vector from a list

Use the “unlist()” function to convert the relevant list into an R vector. This function uses an R List as transfer parameter, and transforms this into a vector. The list thus passed to the function can also be multidimensional. We especially recommend that you use the “unlist()” function if you need to transpose complex hierarchies into simple vector form.

liste <- list(1, 2, 3, 4)
vektor <- unlist(liste)

R

If you transform a list composed of different data types into a vector, it risks behaving in an undesirable and undefined way, because you should know that in R, vectors are only able to store, by default, data of the same type. We therefore advise you to only call “unlist()” if the the elements present in your list are data of the same type.

The following code example shows a list in which all values ​​are assimilated to the same data type, “character” for storage in the R vector.

liste <- list(1, TRUE, "Hello World!", c(1,2,3,4))
vector <- unlist(liste)

R

R List to DataFrame: create a DataFrame from a list

In R, DataFrames are used in particular to present data sets in tabular form. They are especially useful for converting multidimensional lists into R DataFrames. Please note: in this case, the lists must contain the same number of elements. To convert an R List into a DataFrame, R offers the “data.frame()” function. You must enter the list to convert as a transfer parameter.

person <- list(nom=c("Paul", "Mia", "Sarah"), age=c(27, 32, 23))
tabelle <- data.frame(person)
print(tabelle)

R

The DataFrame created from the code above looks like this:

nom age
1  Paul    27
2  Mia    32
3  Sarah    23

R

Access to elements in R Lists

To access the items in your lists, you can choose to use the index or, where applicable, the name of the items in question.

Access using the index

To access list elements, you must use the R operator “ [[]]” and insert the corresponding index or name.

liste <- list(1, TRUE, "Hello World!", c(1,2,3,4))
print(liste[[2]])

R

This code then returns the result “TRUE”, i.e. the second element in the list. Warning: in R, theindexing starts at 1. Do you already have experience in this field and have you already learned to program? Be careful, you are likely to make mistakes! Indeed, indexing starts at 0 in the majority of other programming languages.

Access using the element name

As we said above, you can name the elements of your R List. Subsequently and if necessary, you can use these names to access the corresponding elements. You have two different solutions for accessing elements by name: you can choose to surround the name of the element you are looking for (as an R String) with double brackets, or use the dollar symbol (“$ “) followed by the name in question. Below is sample code demonstrating these methods:

person <-list(nm = "Paul Michel", age = 27, hobbies = c("Programmation, Cinéma"))
print(person [["nom"]])
print(person$nom)

R

In these two cases, the “print” instruction displays the ““Paul Michel”” result on the screen.

List of functions in R: what are the main functions?

Do you work with lists? Several R functions may be useful to you if you are looking to perform certain operations.

“append()”

The “append()” function allows you to add elements to the end of your list. The first transfer parameter of the function must correspond to the list to which the element is to be added, itself specified as the second transfer parameter.

liste <-list(1,2,3,4)
append(liste, 5)

R

If the element you want to insert has a name, you just have to indicate it when calling the function.

In R, the “append()” list function is not the only solution available to you for adding elements to your list. Most of the time, if you enter an index using double brackets (for an index that does not yet exist), this saves you time:

liste <-list(1,2,3,4)
liste[[5]] <-5

R

“length()”

If you call the R command “length()”, you will know the number of elements present in your list. For this function, you must specify the list whose length you are interested in as a transfer parameter.

liste <- list(1,2,3,4)
print(length(liste))

R

Concatenation with “c()”

Using R, you can merge two lists so that they form one. Here it is appropriate to use the “c()” function and enter the two lists as parameters; it will then return a concatenated list.

liste1 <- list(1,2,3,4)
liste2 <- list(5,6,7,8)
liste_contact <- c(liste1, liste2)

R

“names()”

Have you created an R List with names? Use the R list function “names()” to display the names of objects in your list. You must provide a list to this function as a transfer parameter.

person <-list(nm = "Paul Michel", age = 27, hobbies = c("Programmation, Cinéma"))
identifiant <- names(person)

R

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