![]() |
With so much data around us in today’s world, dealing with them becomes tough. In this case, the Dplyr data frame package from R acts as a lifesaver and that package stands out as a powerful and versatile tool. for data manipulation. In R Programming Language package has many functions and among them, slice() is particularly useful for extracting specific rows from any data frame based on their indexes (positions). In this article, we will look at the details of this slice() function and explore how can it help in the data manipulation process. Introduction to Slice() function in dplyrThe slice() function in dplyr allows users to subset data frames by selecting specific rows based on their indexes or positions. In simple words, as the word slice suggests, it is like taking a piece or part of a data frame using the position of data. Using this function, we could get any desired part of the dataframe and we could use that part for some other purposes. This function has a really simple syntax and integrates easily with other dplyr functions, which makes it an invaluable tool for data wrangling tasks. The basic syntax for the slice() function can be written as.
Now, let us look into how we can use this slice() function by looking at the steps. Steps to implement Slicing in RNow, let us know the steps to implement this slice() method in R. Step 1: Install and load the packakgesThe first step is to install and load the dplyr package which has this function into the environment.
Step 2: Data preparationIn this step, we now need data to slice. So, this step is data preparation. df <- data.frame( The above code shows how to create a basic dataframe with multiple rows and columns in R. We used data.frame() to create it. We can either create our own dataframe or else, we can use an already existing dataset and work on it. Step 3: Slice operationNow it is time to perform slice operation on the data frame.
In this case the operation in slice(), after which the result is stored in new variable df2. The parameters inside the slice() function denotes the start and end indexes for slicing, where both are included. Note: In the above code, we used an operator called pipe (%>%) in dplyr package. The operator takes the input from the left hand dataframe and performs the operation on the right side. These were the steps involved in using the slice() function. Now, let us dive into types of slice() functions in dplyr package which make data analysis much more simpler. Types of Slicing MethodsThere are various other slicing methods in dplyr package, that are available to cater to different needs, like selecting rows of a dataframe by index, choosing the first or last rows, extracting the minimum or maximum values from a column, or randomly sampling rows from a dataset. Now, let us see each type in detail with an example. I will use the dataset called ‘mtcars’ which is available by default in R studio to demonstrate each slicing method. 1. slice(): Slices the dataframe by row indexThis function is helpful to slice the dataframe by using the row indexes. We can either slice one row, rows in a range or even rows which are non-continuous, i.e, multiple rows. Below is the syntax for it.
R
Output: The 2nd row is: 2. slice_head(): Select the top rowsThis function helps us to get the top part of any dataframe. Here we can even specify how many rows in top we actually want to slice using an argument called ‘n’.
R
Output: The first 4 rows are: 3. slice_tail(): Select the bottom rowsThis function is similar to the above but is for the bottom part of the dataframe.
R
Output: The last 4 rows are: 4. slice_min(): Select the minimum of a columnAs the function specifies, it gets the rows with minimum values from the dataframe, where we can specify based on the order of which column we need to slice the dataframe.
R
Output: The row with the lease mpg: 5. slice_max(): Select the maximum of a columnThis function is opposite to the slice_min() function. This selects the rows with maximum values based on the order of one particular column.
R
Output: The row with the maximum disp 6. slice_random(): Select random rowsAs the term random says that this method slices random rows from the dataframe. Here, also a parameter called ‘n’ can be given to specify how many rows must be selected.
R
Output: 3 random rows are: More Examples on Slice()Now, let us look at the examples of the slicing and where it is used in data analysis. For suppose we wanted to find out the rows with a particular condition. Let’s say we have a dataframe of student names and their scores. And we need to get all the student names with their score whose score is above 85%. So, firstly let us create a dataframe, after which we are going to write the slice function to slice the dataframe based on the condition which is score>85. R
Output: ID Name Score We created a dataframe called class_score with columns like id, names, and scores respectively. Then we used the pipe operator as discussed before which uses the class_score dataframe to slice from it and store in new variable. The parameter of the slice() function is which(). This part of code returns the indices where the condition Score > 85 is true. So, it returns the position of elements that are True. Hence, in this way we can use the slice function. Example 2: Let us now take the example dataset of cricket teams and their scores. Our task is to find the top teams from the dataframe. R
Output: Team Score Here, we took a dataframe for the cricket teams and their scores and we tried to find the top scorers. Here also we used the pipe operator from dplyr. Firstly, we arranged the dataframe in decreasing order according to the column ‘Score’, after which we used the slice() function to get the top three scores. ConclusionThe slice() function in dplyr package of R is really a powerful tool to extract specific rows according to our need from any dataframe based on their positions. It is really easy and simple to use function which can be mastered by anyone with practice. By mastering this function, data anlaysts and scientists can improve their data wrangling tasks to unlock deeper insights from the datasets. FAQs on Slice() functionWhat does slice() function do in R?
How does slice() differ from other functions like filter() in dplyr?
Does slice() modify the original data frame?
Can I use negative positions with slice()?
What happens if I provide positions that are out of range?
|
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |