How to Use the drop() Function in R
Last Updated :
09 Sep, 2024
In R Language the drop()
function is used to eliminate redundant dimensions of an object, such as dropping dimensions from a matrix, data frame, or array. The function simplifies the structure by reducing the number of dimensions when the result has only one row one column, or both. This can be particularly useful when working with subsets of matrices or arrays, where R may automatically retain a higher dimension structure even when it's not necessary.
The basic syntax of the drop()
function is:
drop(x)
x
: The object (matrix, array, or data frame) from which dimensions should be dropped.
When to Use drop()
The drop()
function is commonly used when you want to reduce the dimensions of an array or matrix that results from subsetting. Without explicitly using drop()
, R may retain the original dimensions, even when they are no longer needed.
Example 1: Using drop()
with a Matrix
Consider a matrix m
with 3 rows and 3 columns.
R
# Create a 3x3 matrix
m <- matrix(1:9, nrow = 3, ncol = 3)
print(m)
# Subset the first row and first column
element <- m[1, 1, drop = FALSE]
print(element)
Output:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[,1]
[1,] 1
Here, the result retains the matrix structure with one row and one column. To remove this unnecessary dimension, use the drop()
function:
Example 2: Using drop()
with an Array
Consider a 3-dimensional array.
R
# Create a 3-dimensional array
arr <- array(1:12, dim = c(2, 2, 3))
print(arr)
# Use drop() to reduce dimensions
sub_arr_dropped <- drop(arr[1, , ])
print(sub_arr_dropped)
Output:
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
, , 3
[,1] [,2]
[1,] 9 11
[2,] 10 12
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
Example 3: Using drop()
with Data Frames
While drop()
is most commonly used with matrices and arrays, it can also be used to simplify data frames when necessary.
R
# Create a data frame
df <- data.frame(A = 1:3, B = 4:6)
print(df)
# Subset a single column
subset_column <- df[, "A"]
print(subset_column)
# Subset a single column but retain data frame structure
subset_column_df <- df[, "A", drop = FALSE]
print(subset_column_df)
# Use drop() to simplify the structure
simplified_column <- drop(subset_column_df)
print(simplified_column)
Output:
A B
1 1 4
2 2 5
3 3 6
[1] 1 2 3
A
1 1
2 2
3 3
A
1 1
2 2
3 3
- Using
drop = FALSE
can force R to retain the data frame structure:
Conclusion
The drop()
function is a useful tool in R for reducing unnecessary dimensions in matrices, arrays, and data frames. It simplifies the structure of an object by eliminating dimensions that are no longer needed after subsetting. This function becomes particularly handy when dealing with high-dimensional data and helps make the results easier to interpret.
Similar Reads
How to use the source Function in R
In this article, we will be looking at the practical implementation of the source function in the R programming language. Source Function: Source function in R is used to use functions that are created in another R script. The syntax of this function is given below: source("Users/harsh/Desktop/Geeks
2 min read
How to Use ColMeans Function in R?
In this article, we will discuss how to use the ColMeans function in R Programming Language. Using colmeans() function The colmean() function call be simply called by passing the parameter as the data frame to get the mean of every column present in the data frame separately in the R language. Synta
3 min read
How to use Summary Function in R?
The summary() function provides a quick statistical overview of a given dataset or vector. When applied to numeric data, it returns the following key summary statistics: Min: The minimum value in the data1st Qu: The first quartile (25th percentile)Median: The middle value (50th percentile)3rd Qu: Th
2 min read
How to Use read.delim in R?
In this article, we will learn how to use the read.delim() in the R Programming Language. Example 1: Using read.delim() function to read a space-separated text file The read.delim() function is used to read delimited text files in the R Language. It doesn't need any external package to work. This fu
3 min read
How to use the drop method in Lodash ?
The Drop method in Lodash removes elements from the beginning of arrays or characters from the start of strings. You can implement the lodash drop method as implemented in the below approaches. Table of Content Removing elements from the beginning of an array using dropRemoving string characters fro
2 min read
How to Use lm() Function in R to Fit Linear Models?
In this article, we will learn how to use the lm() function to fit linear models in the R Programming Language. A linear model is used to predict the value of an unknown variable based on independent variables. It is mostly used for finding out the relationship between variables and forecasting. The
4 min read
How to Use do.call in R
In R Programming language the do.call() function is used to execute a function call using a list of arguments. This can be particularly useful when we have a function and its arguments stored in separate objects (e.g., in a list) and we want to apply the function to these arguments. Syntax do.call(f
3 min read
How to Generate a Sample Using the Sample Function in R?
In this article, we will discuss how to generate a sample using the sample function in R. Sample() function is used to generate the random elements from the given data with or without replacement. Syntax: sample(data, size, replace = FALSE, prob = NULL) where, data can be a vector or a dataframesize
2 min read
How to Use dplyr to Generate a Frequency Table in R
The frequency table in R is used to create a table with a respective count for both the discrete values and the grouped intervals. It indicates the counts of each segment of the table. It is helpful for constructing the probabilities and drawing an idea about the data distribution. The dplyr package
3 min read
Slice() Function In R
Slice() is a function in R Programming Language that is used to manipulate data frames and datasets using a simple syntax. It is used to make subsets of data frames, it allows data manipulation. These datasets can be sliced using the slice() function. R slice() function syntax:Syntax : slice(.data,
8 min read