Please enable JavaScript.
Coggle requires JavaScript to display documents.
Course 2: R Programming (Subsetting (Matrices (By default, if a single…
Course 2: R Programming
Subsetting
[
the single bracket always returns an object of the same class as the original; can be used to subset more than 1 element (has one exception)
-
[[
the double bracket is used to subset elements of a list or data frame. it can be used to extract only a single element and the class of the returned object may not be the same class as the original
-
-
Lists
x = an r object
x <- list(foo = 1:4, bar = 0.6)
x[1]
this returns a list, foo with the sequence 1through 4 in it
-
-
x[["bar"]]
can also feed a string into the double bracket subset. this also just returns the value that "bar" is associated with
-
-
Matrices
-
-
-
By default, if a single element is subset using the single bracket, a vector of length 1 is returned. NOT a 1x1 matrix. even though usually the single bracket notation would mean an object of the same class gets returned. this is the exception
-
another exception, is that by default if you subset a single row then this also gets returned as a vector. not a 1 by ncol matrix. this behavior can also be turned off by setting drop = FALSE
-