I have a very deep list of lists in R. Now I want to print this list to the standard output to get a better overview of the elements. It should look like the way the StatET plugin for eclipse shows a list.
Example list:
l6 = list()
l6[["h"]] = "one entry"
l6[["g"]] = "nice"
l5 = list()
l5[["e"]] = l6
l4 = list()
l4[["f"]] = "test"
l4[["d"]] = l5
l3 = list()
l3[["c"]] = l4
l2 = list()
l2[["b"]] = l3
l1 = list()
l1[["a"]] = l2
This should print like:
List of 1
$ a:List of 1
..$ b:List of 1
.. ..$ c:List of 2
.. .. ..$ f: chr "test"
.. .. ..$ d:List of 1
.. .. .. ..$ e:List of 2
.. .. .. .. ..$ h: chr "one entry"
.. .. .. .. ..$ g: chr "nice"
I know this is possible with recursion and the deepness.
But is there a way to do this with the help of rapply
or something like that?
Thanx in advance,
Martin
The list is one of the most versatile data types in R thanks to its ability to accommodate heterogenous elements. A single list can contain multiple elements, regardless of their types or whether these elements contain further nested data. So you can have a list of a list of a list of a list of a list …
Almost all data in R is stored in a vector, or even a vector of vectors. A list is a recursive vector: a vector that can contain another vector or list in each of its elements. Lists are one of the most flexible data structures in R.
To add an item to a list in R programming, call append() function and pass the list and item as arguments in the function call. In this tutorial, we will learn how to add or append an element to a list in R, using append() function, with the help of example programs.
I think you could get what you want by giving
str(l1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With