如何在R中剪切具有最小值的数据框列?
要切割具有最小值的数据框列,我们需要在 min 函数的帮助下使用 cut 函数指定最小值并将include.lowest参数设置为 TRUE。我们还可以指定最大值,以便在切割列时也考虑最大值。
示例 1
以下代码段创建了一个示例数据框 -
x<-rpois(20,5)df1<-data.frame(x)
df1
创建以下数据框 -
x1 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
要使用最小值和最大值切割 x 列,请将以下代码添加到上面的代码段中 -
cut(df1$x,c(min(df1$x),3:10,max(df1$x)),include.lowest=TRUE)输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
[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]
示例 2
以下代码段创建了一个示例数据框 -
y<-rpois(20,10)df2<-data.frame(y)
df2
创建以下数据框 -
y1 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
要使用最小值和最大值切割 y 列,请将以下代码添加到上面的代码段中 -
cut(df2$y,c(min(df2$y),4:15,max(df2$y)),include.lowest=TRUE)输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
[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]
示例 3
以下代码段创建了一个示例数据框 -
df3<-data.frame(z)df3
创建以下数据框 -
z1 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
要使用最小值和最大值切割列 z,请将以下代码添加到上面的代码段中 -
cut(df3$z,c(min(df3$z),1:5,max(df3$z)),include.lowest=TRUE)输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
[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]
以上是 如何在R中剪切具有最小值的数据框列? 的全部内容, 来源链接: utcz.com/z/335454.html