Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List assignment for list with greater than three nesting

Tags:

list

r

nested

I have not been able to find a fix for this error. I have implemented work-arounds before, but I wonder if anyone here knows why it occurs.

the following returns no error as expected

q <- list()
q[["a"]][["b"]] <- 3
q[["a"]][["c"]] <- 4

However, when I add another level of nesting I get:

q <- list()
q[["a"]][["b"]][["c"]]<- 3
q[["a"]][["b"]][["d"]] <- 4

Error in q[["a"]][["b"]][["d"]] <- 4 : more elements supplied than there are to replace

To make this even more confusing if I add a fourth nested list I get:

q <- list()
q[["a"]][["b"]][["c"]][["d"]] <- 3
q[["a"]][["b"]][["c"]][["e"]] <- 4

Error in *tmp*[["c"]] : subscript out of bounds

I would have expected R to return the same error message for the triple nested list as for the quadruple nested list.

I first came across this a few months ago. I am running R 3.4.3.

like image 912
NicoFish Avatar asked Mar 02 '18 03:03

NicoFish


People also ask

How do you add a list to a nested list?

To add new values to the end of the nested list, use append() method. When you want to insert an item at a specific position in a nested list, use insert() method. You can merge one list into another by using extend() method. If you know the index of the item you want, you can use pop() method.

What is nested list write an example?

A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list. If we print( nested[3] ), we get [10, 20] .

Is a nested list a list?

A nested list is a list of lists, or any list that has another list as an element (a sublist). They can be helpful if you want to create a matrix or need to store a sublist along with other data types.

What is nested list Class 8?

A nested list is simply a list that occurs as an element of another list (which may of course itself be an element of another list, etc.). Common reasons nested lists arise are: They're matrices (a list of rows, where each row is itself a list, or a list of columns where each column is itself a list).


1 Answers

If we check the str(q) from the first assignment, it is a list with a single element 'a'. On subsequent assignment, it is creating a named vector rather than a list.

q <- list()
q[["a"]][["b"]] <- 3
q[["a"]][["c"]] <- 4
str(q)
#List of 1
# $ a: Named num [1:2] 3 4
#  ..- attr(*, "names")= chr [1:2] "b" "c"

is.vector(q$a)
#[1] TRUE

If we try to do an assignment on the next level, it is like assignment based on indexing the name i.e. 'b' which is empty and assign value on 'c'. The option would be to create a list element by wrapping the value with list

q <- list()
q[["a"]][["b"]][["c"]]<- list(3)
q[["a"]][["b"]][["d"]] <- list(4)

It returns the structure with 'q' as a list of 1 element i.e. 'a', which is again a list of length 1 ('b') and as we assign two values '3' and '4' for 'c' and 'd', it is a list of 2 elemeents

str(q)
#List of 1
# $ a:List of 1
#  ..$ b:List of 2
#  .. ..$ c:List of 1
#  .. .. ..$ : num 3
#  .. ..$ d:List of 1
#  .. .. ..$ : num 4

By this way, we can nest 'n' number of lists

q <- list()
q[["a"]][["b"]][["c"]][["d"]] <- list(3)
q[["a"]][["b"]][["c"]][["e"]] <- list(4)

Note: It is not clear about the expected output structure

like image 70
akrun Avatar answered Oct 26 '22 12:10

akrun