2

What I have been teaching is that vectors are one dimensional, and data frames are two-dimensional. Hence, indexing on vectors requires one number - e.g., v[1] - and indexing on data frames requires two numbers - e.g., df[1, 1].

I am learning from https://stackoverflow.com/a/13635514/3625022 that df[1], for example, actually shows the first column of df. For example,

> x <- data.frame(x = 1, y = 2, z = 3)
> x[1]
  x
1 1
> x[,1]
[1] 1

I notice that these are slightly different, as x[,1] likely refers only to the vector of entries in the first column, whereas x[1] includes the row and column names.

Regardless, this contradicts the message that I have been stating - that data frames need two numbers for indexing. Is this statement incorrect? If it is correct, how does one explain to those new to R why x[1] gives the first column of x?

Community
  • 1
  • 1
Clarinetist
  • 1,097
  • 18
  • 46
  • 1
    data.frame is a class that inherits from class list. Thus, list subsetting works for data.frames. – Roland Dec 20 '16 at 19:52
  • Note that `x[1]` is a `data.frame`, not a vector. – Joshua Ulrich Dec 20 '16 at 19:53
  • 2
    Everything in R is one-dimensional (that I know of). Data.frames are lists of columns; matrices are vectors shaped to look like something else; etc. – Frank Dec 20 '16 at 19:54
  • 2
    @Frank Matrices are vectors with a dim attribute. No "shape" involved. – Roland Dec 20 '16 at 19:58
  • @Roland Thanks for clarifying for the OP. I was perhaps explaining too loosely. – Frank Dec 20 '16 at 20:01
  • 1
    @Frank If OP is teaching it would be advantageous to understand the internal implementation of R data structures. – Roland Dec 20 '16 at 20:05
  • Thanks @Roland. Where can I learn more about these "inherited" classes (textbook, perhaps)? I'm admittedly not familiar with OOP (perhaps unsurprisingly, most of my work is procedural), but I am looking to learn more as time goes on. I have my hands on "Advanced R" by Wickham, and it looks like I may consider purchasing "Extending R" by Chambers in the future. – Clarinetist Dec 20 '16 at 20:09
  • 2
    The [intro R manual](https://cran.r-project.org/doc/manuals/r-release/R-intro.html), is pretty dry an much of it you may already know, but it is full of useful insights. – lmo Dec 20 '16 at 20:17

1 Answers1

4

Data frames are lists. A special type of list, to be sure, but they are lists. x[1] is the first element of the list. x[[1]] is the contents of the first element of the list, as is x[,1] when the list is a data frame. Compare the output to an explicit list:

> x <- data.frame(x = 1, y = 2, z = 3)
> x[1]
  x
1 1
> x[[1]]
[1] 1
> x[,1]
[1] 1
> y = list(x=1, y=2, z=3)
> y[1]
$x
[1] 1

> y[[1]]
[1] 1
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79