Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to visualize a list of lists of lists of ... in R?

Tags:

r

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

like image 963
Martin Avatar asked May 18 '10 14:05

Martin


People also ask

Can you have a list of lists in R?

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 …

Can you have a vector of lists in R?

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.

How do I add a list to a list 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.


1 Answers

I think you could get what you want by giving

str(l1)
like image 126
George Dontas Avatar answered Oct 27 '22 19:10

George Dontas