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 −

cut(df1$x,c(min(df1$x),3:10,max(df1$x)),include.lowest=TRUE)

Output

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

[1] (5,6] (6,7] (3,4] [2,3] (5,6] [2,3] (6,7] [2,3] [2,3]
[10] [2,3] (4,5] (3,4] [2,3] (7,8] (10,11] [2,3] (5,6] [2,3]
[19] [2,3] (3,4]
Levels: [2,3] (3,4] (4,5] (5,6] (6,7] (7,8] (8,9] (9,10] (10,11]

Example 2

Following snippet creates a sample data frame −

y<-rpois(20,10)
df2<-data.frame(y)
df2

The following dataframe is created −

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

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

cut(df2$y,c(min(df2$y),4:15,max(df2$y)),include.lowest=TRUE)

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]
raja
Published on 09-Nov-2021 07:38:12

Advertisements