Friday, April 5, 2019
r - The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
Answer
Answer
R provides two different methods for accessing the elements of a list or data.frame- the []
and [[]]
operators.
What is the difference between the two? In what situations should I use one over the other?
Answer
The R Language Definition is handy for answering these types of questions:
R has three basic indexing operators, with syntax displayed by the following examples
x[i]
x[i, j]
x[[i]]
x[[i, j]]
x$a
x$"a"
For vectors and matrices the[[
forms are rarely used, although they have some slight semantic differences from the[
form (e.g. it drops any names or dimnames attribute, and that partial matching is used for character indices). When indexing multi-dimensional structures with a single index,x[[i]]
orx[i]
will return thei
th sequential element ofx
.
For lists, one generally uses[[
to select any single element, whereas[
returns a list of the selected elements.
The[[
form allows only a single element to be selected using integer or character indices, whereas[
allows indexing by vectors. Note though that for a list, the index can be a vector and each element of the vector is applied in turn to the list, the selected component, the selected component of that component, and so on. The result is still a single element.
plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV
In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...
-
This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programm...
-
I need to do the following: My current address looks like: https://www.domain.com I want to redirect with htaccess: www.domain.com TO https:...
-
String response = getResultForRequest(url); Where 'url' is JSON formatted which return bunch of data using http GET method. public s...
No comments:
Post a Comment