Split DataFrame into Custom Bins in R
Last Updated :
14 Feb, 2022
In this article, we are going to see how to split dataframe into custom bins in R Programming Language.
The cut() method in base R is used to first divide the range of the dataframe and then divide the values based on the intervals in which they fall. Each of the intervals corresponds to one level of the dataframe. Therefore, the number of levels is equivalent to the length of the breaks argument in the cut method.
Syntax: cut(x, breaks, labels = NULL)
Arguments :
- x - Numeric vector to be divided
- Breaks - A vector containing the intervals
- Labels - labelling of the groups
Example 1: Split dataframe into Custom Bins
R
# creating a dataframe
data_frame <- data.frame(col1 = c(1:10),
col2 = letters[1:10],
col3 = c(rep(TRUE,4),
rep(FALSE,6)))
print("Original DataFrame")
print(data_frame)
# getting rows of data
rows <- nrow(data_frame)
# custom bins
bins <- cut(1:rows,
breaks = c(0,6,rows
))
level_bins <- levels(bins)
# printing the subsets of dataframe
for(i in 1:length(level_bins)) {
assign(paste0("data_frame_", i),
data_frame[bins == levels(bins)[i], ])
}
# retrieving dataframe subsets
print("DataFrame Subset 1")
print(data_frame_1)
print("DataFrame Subset 2")
print(data_frame_2)
 Â
Output:
Â
Â
Example 2: Illustrates the usage where three breakpoints are specified, thereby, dividing the rows into three subsets of the original dataframe.
Â
R
# creating a dataframe
data_frame <- data.frame(col1 = c(1:10),
col2 = letters[1:10],
col3 = c(rep(TRUE,4),
rep(FALSE,6)))
print("Original DataFrame")
print(data_frame)
# getting rows of data
rows <- nrow(data_frame)
# custom bins
bins <- cut(1:rows,
breaks = c(0,2,4,rows
))
level_bins <- levels(bins)
# printing the subsets of dataframe
for(i in 1:length(level_bins)) {
assign(paste0("data_frame_", i),
data_frame[bins == levels(bins)[i], ])
}
# retrieving dataframe subsets
print("DataFrame Subset 1")
print(data_frame_1)
print("DataFrame Subset 2")
print(data_frame_2)
print("DataFrame Subset 3")
print(data_frame_3)
 Â
Output:
Â
Â
Example 3: The cut method may also specify the number of equal parts in which the dataframe is to be divided. This is specified as the second argument of the method. The dataframe is divided into those numbers of equivalent parts and correspondingly assigned the names specified. The following code divides the dataframe into 5 custom bins of equal sizes :
Â
R
# creating a dataframe
data_frame <- data.frame(col1 = c(1:10),
col2 = letters[1:10],
col3 = c(rep(TRUE,4),
rep(FALSE,6)))
print("Original DataFrame")
print(data_frame)
# getting rows of data
rows <- nrow(data_frame)
# custom bins
bins <- cut(1:rows,5)
level_bins <- levels(bins)
# printing the subsets of dataframe
for(i in 1:length(level_bins)) {
assign(paste0("data_frame_", i),
data_frame[bins == levels(bins)[i], ])
}
# retrieving dataframe subsets
print("DataFrame Subset 1")
print(data_frame_1)
print("DataFrame Subset 2")
print(data_frame_2)
print("DataFrame Subset 3")
print(data_frame_3)
print("DataFrame Subset 4")
print(data_frame_4)
print("DataFrame Subset 5")
print(data_frame_5)
 Â
Output:
Â
Â
Similar Reads
How to Split Column Into Multiple Columns in R DataFrame? In this article, we will discuss how to split a column from a data frame into multiple columns in the R programming Language. Method 1: Using str_split_fixed() function of stringr package library To split a column into multiple columns in the R Language, We use the str_split_fixed() function of the
3 min read
Split DataFrame Variable into Multiple Columns in R In this article, we will discuss how to split dataframe variables into multiple columns using R programming language. Method 1: Using do.call method The strsplit() method in R is used to split the specified column string vector into corresponding parts. The pattern is used to divide the string into
3 min read
How to split DataFrame in R In this article, we will discuss how to split the dataframe in R programming language. A subset can be split both continuously as well as randomly based on rows and columns. The rows and columns of the dataframe can be referenced using the indexes as well as names. Multiple rows and columns can be r
4 min read
How to Select DataFrame Columns by Index in R? In this article, we will discuss how to select columns by index from a dataframe in R programming language. Note: The indexing of the columns in the R programming language always starts from 1. Method 1: Select Specific Columns By Index with Base R Here, we are going to select columns by using index
2 min read
Convert dataframe column to list in R In this article, we will learn how to convert a dataframe into a list by columns in R Programming language. We will be using as.list() function, this function is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and data frames. Syntax: as.list( object ) Parameter
2 min read