 ``````> g <- "My First List"
> h <- c(25,26,18,39)
> j <- matrix(1:10, nrow=5)
> k <- c("one","two","three")
> mylist <- list(title=g, ages=h,j,k)
> mylist
\$title
 "My First List"

\$ages
 25 26 18 39

[]
[,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

[]
 "one"   "two"   "three"
``````

I don't understand [] and [] above. I think g is [], then h,j,k are 2,3,4?

Yes. You named the first two elements of the list, so they are shown as title and ages. The remaining elements have no name, so they are labeled with their position in the list. The double bracket returns the nth element of the list.

``````mylist[]
``````

would return

`````` "My First List"
``````

mylist has four elements. The first one is named test and it contains a character vector with one element. The second is named ages and it contains a vector of four numbers. The third is unnamed and it contains a matrix or 10 elements. The fourth is unnamed and it contains a character vector of three elements.
Try this

``````mylist <- list(title=g, ages=h, third = j, fourth =  k)
mylist
``````

you will see that [] is replaced by \$third and [] is replaced by \$fourth.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

But where is ages[]??? I only see ages[] [] []

Aha, I see. Very clear explanation! Thank you so much!