Hello,
I am still slightly confused about the difference in output between single square brackets [ ] and double brackets [[ ]] to extract an element or column from a vector or dataframe. See the example below where we get kind of the same results but with single square brackets I get a column of numbers and with [[ ]] we get a horizontal list of numbers...
Does the single square bracket access only one element? What if we use two indices and single square brackets? When using the double square brackets, I get the same results so I am confused...I know that one outputs a list while the other does not...
as a heuristic double square brackets will unbox/unwrap the result it gives you; i.e. you will get to the raw/naked element value from it; single square brackets will generally give you the result boxed/wrapped. either letting it have supplementary info like names attached; or as a list if it was pulled from a list, or a data.frame if it was in a data.frame or a tibble if it was in a tibble etc.
#The usual form of indexing is [. [[ can be used to select a single element dropping names, whereas [ keeps them, e.g
c(abc = 123)[1]
c(abc = 123)[[1]]
# give 1st element but return wrapped in a list
list(1,
2)[1]
# give 1st element unwrapped; i.e. dont have the result in a list
list(1,
2)[[1]]
data.frame(a=1)[1] # a data.frame
data.frame(a=1)[[1]]
tibble(a=1)[1] # a tibble
tibble(a=1)[[1]]