4

R may have its own loigc but list() did not give me what I expected.

l1 <- list(1,2)
$> l1
[[1]]
[1] 1

[[2]]
[1] 2

To retrieve the element, I need to use double-bracket, i.e.,

$> l1[[1]]
[1] 1
$> class(l1[[1]])
"numeric"

Single-bracket gives me, a sub-list (which is also a list object):

$> l1[1]
l1[[1]]
[1] 1
$> class(l1[1])
"list"

I am not saying this is wrong; this isn't what I expected because I was trying to create a 1-dimensional list whereas what I actually get is a nested list, a 2-dimensional object.

What is the logic behind this behaviour and how do we create an OO type list? i.e., a 1-dimensional data structure?

The behaviour I am expecting, with a 1 dimensional data structure, is:

$> l1[1]
[1] 1
$> l1[2]
[2] 2
stucash
  • 1,078
  • 1
  • 12
  • 23
  • 2
    Lists are 1-dimensional, not 2-dimensional, they just have no restrictions on what can go in each element. R's atomic vectors are also 1-dimensional data structures, but they are restricted in that every element is the same class. You can use `c` or other shortcut functions to create numeric vectors, e.g., `c(1, 2)` or `1:2`. – Gregor Thomas Mar 21 '22 at 13:03
  • @GregorThomas ok with JBGruber's answer below I take your point as those 2 numbers I put in, were actually two vectors in R's language? – stucash Mar 21 '22 at 13:08
  • Based on the updated last paragraph, I think you want to create a vector, not a list: `l1 <- c(1, 2)` – JBGruber Mar 21 '22 at 13:13
  • @JBGruber I agree, especially with your reference to the indexing guide. you and Gregor helped my understand that R actually takes each element in a `list` as vector, regardless what I see it as, intuitively; it may be a number, but R takes it as vector as long as that element goes into a `list`. Can you kindly add this comment into your answer I'll then accept it as my answer. Thanks a lot :) – stucash Mar 21 '22 at 13:17
  • 2
    Yes, `list(1, 2)` is a 2-element list. The first element of the list is a 1-element numeric vector containing 1. The second element of the list is a 1-element numeric vector containing 2. Contrast with JBGruber's answer, `list(c(1, 2))` is a 1-element list, where the only element is the length-2 vector containing 1 and 2. R is unusual in that its most basic data type **is a vector**, so you don't need a more traditional "list" type object to store more than one value. [The R for Data Science chapter on Vectors](https://r4ds.had.co.nz/vectors.html) is maybe the reading you're looking for. – Gregor Thomas Mar 21 '22 at 13:21
  • That was the chapter I was looking for Gregor! I linked the more advanced chapter. – JBGruber Mar 21 '22 at 13:24
  • @stucash As to *"R actually takes each element in a list as vector, regardless what I see it as"* - In R **numbers are vectors**. A single number is a numeric vector of length 1, and in these examples you're right that the elements we've been putting the lists are vectors. But lists can contain literally anything - a single element of a `list` could be a vector, a data frame, another list, a function, some other object... – Gregor Thomas Mar 21 '22 at 14:59
  • @GregorThomas thanks very much for the elaboration; I am now on board with the idea that numbers in R are vectors: a number is a vector in R. This is something new to me, I'll need to dig further into the fundamentals of R later. – stucash Mar 21 '22 at 15:05

1 Answers1

5

If you want to create a list with the two numbers in one element, you are looking for this:

l1 <- list(c(1, 2))
l1
#> [[1]]
#> [1] 1 2

Your code basically puts two vectors of length 1 into a list. To make R understand that you have one vector, you need to combine (i.e., c()) the values into a vector first.

This probably becomes clearer when we create the two vectors as objects first:

v1 <- 1
v2 <- 2
l2 <- list(v1, v2)
l2
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2

If you simply want to store the two values in an object, you want a vector:

l1 <- c(1, 2)
l1
#> [1] 1 2

For more on the different data structures in R I recommend this chapter: http://adv-r.had.co.nz/Data-structures.html

For the question about [ and [[ indexing, have a look at this classic answer: https://stackoverflow.com/a/1169495/5028841

JBGruber
  • 11,727
  • 1
  • 23
  • 45