coat
1 calico
2 black
3 tabby
The single brace [1]
returns the first column of the data frame as a one-column data frame (this will make more sense once we introduce lists).
The $
notation, however, lets us extract a column by name as a vector.
[1] "calico" "black" "tabby"
As for vectors, we can also use the single brace notation with named indexing:
coat
1 calico
2 black
3 tabby
Here we are using a single brace ["coat"]
replacing the index number with the column name. Like example 1, the returned object is a data frame.
Next,
This example uses a single brace, but this time we provide row and column coordinates. The returned object is the value in row 1, column 1. The object is a vector of type character.
What about:
[1] "calico" "black" "tabby"
Like the previous example, we use single braces and provide row and column coordinates. The row coordinate is not specified, R interprets this missing value as all the elements in this column and returns them as a vector.
coat weight likes_string
1 calico 2.1 1
Again we use the single brace with row and column coordinates. The column coordinate is not specified. The return value is a list containing all the values in the first row.