| Start |
|---|
| Quick Introduction to Lists In general, most of the objects you would interact with as a user would tend to be a vector; e.g numeric vector, logical vector. These objects can only take in a single type of variable (a numeric vector can only have numbers inside it). A list would be able to store any type variable in it, making it to the generic object that can store any type of variables we would need. Example of initializing a list In order to understand the data that was defined in the list, we can use the str function. Subsetting of lists distinguishes between extracting a slice of the list, i.e. obtaining a list containing a subset of the elements in the original list, and extracting a single element. Using the To obtain a single element use List entries may be named: The entries in named lists can be accessed by their name instead of their index. Alternatively the This has the advantage that it is faster to type and may be easier to read but it is important to be aware of a potential pitfall. The Lists can be particularly useful because they can store objects of different lengths and of various classes. |
| Quick Introduction to Lists In general, most of the objects you would interact with as a user would tend to be a vector; e.g numeric vector, logical vector. These objects can only take in a single type of variable (a numeric vector can only have numbers inside it). A list would be able to store any type variable in it, making it to the generic object that can store any type of variables we would need. Example of initializing a list In order to understand the data that was defined in the list, we can use the str function. Subsetting of lists distinguishes between extracting a slice of the list, i.e. obtaining a list containing a subset of the elements in the original list, and extracting a single element. Using the To obtain a single element use List entries may be named: The entries in named lists can be accessed by their name instead of their index. Alternatively the This has the advantage that it is faster to type and may be easier to read but it is important to be aware of a potential pitfall. The Lists can be particularly useful because they can store objects of different lengths and of various classes. |
| Serialization: using lists to pass informations There exist cases in which it is necessary to put data of different types together. In Azure ML for example, it is necessary to pass informations from a R script module to another one exclusively throught dataframes. Suppose we have a dataframe and a number: We can access to this information: In order to put different types of data in a dataframe we have to use the list object and the serialization. In particular we have to put the data in a generic list and then put the list in a particular dataframe: Once we have stored the information in the dataframe, we need to deserialize it in order to use it: Then, we can verify that the data are transfered correctly: |
| Reasons for using lists To the average R user, the list structure may appear to be the one of the more complicated data structures to manipulate. There are no guarantees that all the elements within it are of the same type; There is no guaranteed structure of how complicated/non-complicated that the list would be (An element in a list could be a list) However, one of the main reasons when to use lists to use it to pass parameters between functions. In the above example, the returned results are just simple numeric vectors. There is no issues to pass over such simple vectors. It is important to note at this point that generally, R functions only return 1 result at a time (You can use if conditions to return different results). However, if you intend to create a function which takes a set of parameters and returns several type of results such a numeric vector(settings value) and a data frame (from the calculation), you would need to dump all these results in a list before returning it. |
| Convert a list to a vector while keeping empty list elements When one wishes to convert a list to a vector or data.frame object empty elements are typically dropped. This can be problematic which a list is created of a desired length are created with some empty values (e.g. a list with n elements is created to be added to an m x n matrix, data.frame, or data.table). It is possible to losslessly convert a list to a vector however, retaining empty elements: |
Read Article
https://develop.horje.com/learn/1434/reference