# How to cut a data frame column with minimum value in R?

R ProgrammingServer Side ProgrammingProgramming

To cut a data frame column with minimum value, we need to specify the minimum value withing cut function with the help of min function and set the include.lowest argument to TRUE. We can also specify the maximum value so that the maximum value is also taken into account while cutting the column.

## Example 1

Following snippet creates a sample data frame −

x<-rpois(20,5)
df1<-data.frame(x)
df1

The following dataframe is created −

   x
1  6
2  7
3  4
4  3
5  6
6  2
7  7
8  2
9  3
10 2
11 5
12 4
13 3
14 8
15 11
16 2
17 6
18 3
19 3
20 4

To cut the column x with minimum and maximum value, add the following code to the above snippet −



## Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] (15,16] (9,10] (10,11] (4,5] (11,12] (9,10] (7,8] (5,6] (10,11]
[10] [3,4] (12,13] (11,12] (9,10] (10,11] (7,8] (8,9] (6,7] (7,8]
[19] (7,8] (8,9]
13 Levels: [3,4] (4,5] (5,6] (6,7] (7,8] (8,9] (9,10] (10,11] ... (15,16]

## Example 3

Following snippet creates a sample data frame −

df3<-data.frame(z)
df3

The following dataframe is created −

   z
1  3
2  2
3  1
4  2
5  1
6  6
7  2
8  2
9  2
10 1
11 0
12 1
13 1
14 1
15 2
16 1
17 3
18 0
19 4
20 2

To cut column z with minimum and maximum value, add the following code to the above snippet −

cut(df3$z,c(min(df3$z),1:5,max(df3\$z)),include.lowest=TRUE)


## Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] (2,3] (1,2] [0,1] (1,2] [0,1] (5,6] (1,2] (1,2] (1,2] [0,1] [0,1] [0,1]
[13] [0,1] [0,1] (1,2] [0,1] (2,3] [0,1] (3,4] (1,2]
Levels: [0,1] (1,2] (2,3] (3,4] (4,5] (5,6]
Published on 09-Nov-2021 07:38:12