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
?