In this tutorial, we going to look at subsetting in R. R language is so important for data science and this tutorial is a helper for R. If you didn’t read Data Types In R, you can read to prepare.
What Is Subsetting?
Subsetting is a useful indexing function for accessing object elements. For example, we have a list and we want to access the first index in the list, we can use subsetting to access this object. I gave the example Pseudocode below. If you don’t know Pseudocode, you can read the documentation in Wikipedia for this, click the Pseudocode link.
List = [1 , 2 , 3 , 4] List # Access first index
If you don’t understand the above syntax don’t worry we going to examine a lot of examples.
Subsetting Basic Infos
In this part, we going to look at subsetting in vectors so we going to make a simple vector and we subsetting in this vector. If you don’t know vectors you should read Data Types In R, because if you don’t know data types you can’t work with vectors.
Above, firstly, we create a new vector after that we call the first index in the vector so, the value you write in square brackets pulls data from the section in the region you specify. For example, if we wrote the index as 3, we would get the result 3.
Sometimes we need to call multiple data, for this, we can use multi-index, let’s look at this multi-index.
Above, we create a new vector and we are calling indexes 1 to 4. Now, we going to learn to call index by value.
If you assign these indexes to a variable, it will be printed as a bool type, but if it is used as an index of the vector, it will return to its old value like the final output.
Subsetting – Lists And Matrix
Anymore, we know basic subsetting properties, now we going to learn subsetting with lists and matrix. If you don’t know Lists or Matrix you should read Data Types In R.
You can call index in a list with the above methods. Now, we going to look at these methods.
$ syntax -> Used to call variables in a set. (you can’t call normal index you can call variable from set)
X [[“variable”]] -> Used to call variables in a set (you can’t call normal index you can call variable from set)
Anymore we know how to use subsetting with a list, now, we going to pass Matrix subsetting. Before you start, try to create your own examples of past topics.