Subsetting Flashcards
[
Always returns an object of the same clas
Can be used to select more than one element
[[
Used to extract elements of a list or a data frame
Class not necessarily the same as the initial object
$
Used to extract elements by name
Semantics similar to that of [[]]
How do I return the third element of vector x?
x[3]
How do I return the 4th and 5th elements of vector x?
x[4:5]
How do I return the 3rd and 7th elements of vector y?
y[c(3,7)]
How do I return all elements greater than 4 in the vector t?
t[t > 4]
How do I create a logical vector from x, given the condition: x == ‘c’?
u <- x == ‘c’
How do I extract the first row from the matrix m?
m[1,]
How do I return a matrix for element in the first row, second column, instead of returning a vector?
m[1, 2, drop=FALSE]
Note that the drop argument can only be passed to the [] subsetting operation, and has no effect when passed to [[]] (which always returns a vector)
How do you extract the foo element of x by name using the $ syntax?
x$foo
How do apply names to elements in a vector x with two elements?
names(x) <- c(“first”, “second”)
What are two nice things about naming elements, and referencing elements by name?
Makes for cleaner code
Don’t have to remember where in the list/vector an element is
What two things can you do with the [[]] syntax that you cannot do with the $ syntax?
Extract a sequence of elements
Allowing the use of computed indices
If you had a named element in x called “foo”, how would you perform partial matching to return the element “foo” with the [[]] syntax?
x[[“f”, exact = FALSE]]