Wednesday, October 22, 2014

Data Frame Column Vector

We reference a data frame column with the double square bracket "[[]]" operator.
For example, to retrieve the ninth column vector of the built-in data set mtcars, we write mtcars[[9]].
> mtcars[[9]] 
 [1]  1 1 1 0 0 0 0 0 0 0 0 ...
We can retrieve the same column vector by its name.
> mtcars[["am"]] 
 [1]  1 1 1 0 0 0 0 0 0 0 0 ...
We can also retrieve with the "$" operator in lieu of the double square bracket operator.
> mtcars$am 
 [1]  1 1 1 0 0 0 0 0 0 0 0 ...
Yet another way to retrieve the same column vector is to use the single square bracket "[]" operator. We prepend the column name with a comma character, which signals a wildcard match for the row position.
> mtcars[,"am"] 
 [1]  1 1 1 0 0 0 0 0 0 0 0 ...

No comments:

Post a Comment