drop Function in R (Example)
This tutorial demonstrates how to remove redundant dimension information using the drop function in the R programming language.
Table of contents:
It’s time to dive into the example:
Creation of Example Data
The following data is used as basement for this R tutorial:
mat <- matrix(1:4, ncol = 4) # Create example matrix mat # Print example matrix
Table 1 shows that the example data is a matrix object that contains one row and four integer columns.
Let’s check the dimensions of our matrix using the dim() function:
dim(mat) # Check dimensions #  1 4
The dim function confirms what we have already seen in Table 1 – Our data has one row and four columns.
Example: Apply drop() Function to Matrix Object
This section explains how to get rid of unimportant dimensions using the drop command in R.
Let’s apply the drop function to our matrix object:
mat_drop <- drop(mat) # Apply drop function mat_drop # Return output of drop function #  1 2 3 4
As you can see, our matrix with only one row was converted to a vector object.
We can confirm that we have removed the unnecessary dimensions by applying the dim function to our updated data object:
dim(mat_drop) # Check dimensions # NULL
The dim function returns NULL, i.e. no dimensions are left anymore.
In this example, we have applied the drop function to a matrix object. Please note that we could apply the drop function to an array as well.
Video & Further Resources
I have recently released a video on my YouTube channel, which explains the R syntax of this tutorial. You can find the video below:
In addition to the video, you may want to read the related articles on my website.
- Set Row & Column Names of Data with Unknown Dimension
- Access Attributes of Data Object in R
- comment Function in R
- List of R Functions
- R Programming Language
In this R programming tutorial you have learned how to drop redundant extent information. Please tell me about it in the comments, if you have further questions.