如何在R中获得范围因子的最大值和最小值?

时间:2016-08-01 16:13:16

标签: r

您好我是R的新手,我无法知道如何执行以下操作:

我有cut函数的输出,它似乎是类型因子的范围

 [1] (1.1e+05,1.74e+05] (1.1e+05,1.74e+05] [3.09e+04,6e+04]   (1.74e+05,2.4e+05]
 [5] (1.74e+05,2.4e+05] (1.1e+05,1.74e+05] [3.09e+04,6e+04]   [3.09e+04,6e+04]  
 [9] [3.09e+04,6e+04]   [3.09e+04,6e+04]   (6e+04,1.1e+05]    (1.1e+05,1.74e+05]
[13] (6e+04,1.1e+05]    (6e+04,1.1e+05]    (6e+04,1.1e+05]    (6e+04,1.1e+05]   
[17] (6e+04,1.1e+05]    (6e+04,1.1e+05]    (6e+04,1.1e+05]    (6e+04,1.1e+05]   
[21] (6e+04,1.1e+05]    (6e+04,1.1e+05]    (6e+04,1.1e+05]    (6e+04,1.1e+05]   
[25] (6e+04,1.1e+05]    (6e+04,1.1e+05]    (6e+04,1.1e+05]    (1.1e+05,1.74e+05]
[29] (6e+04,1.1e+05]    (1.1e+05,1.74e+05] (1.74e+05,2.4e+05] (6e+04,1.1e+05]   
[33] [3.09e+04,6e+04]   (1.1e+05,1.74e+05] (1.1e+05,1.74e+05] (6e+04,1.1e+05]   
[37] (1.1e+05,1.74e+05] (1.1e+05,1.74e+05] (1.1e+05,1.74e+05] (1.1e+05,1.74e+05]
[41] [3.09e+04,6e+04]   [3.09e+04,6e+04]   (6e+04,1.1e+05]    (1.1e+05,1.74e+05]
[45] (1.1e+05,1.74e+05] (1.1e+05,1.74e+05] (6e+04,1.1e+05]    (6e+04,1.1e+05]   
[49] (6e+04,1.1e+05]    (6e+04,1.1e+05]    (1.1e+05,1.74e+05] (1.1e+05,1.74e+05]
[53] (6e+04,1.1e+05]    (1.1e+05,1.74e+05] (1.74e+05,2.4e+05] [3.09e+04,6e+04]  
[57] [3.09e+04,6e+04]  
4 Levels: [3.09e+04,6e+04] (6e+04,1.1e+05] ... (1.74e+05,2.4e+05]

如何获得该范围内的最大可能值?

1 个答案:

答案 0 :(得分:2)

我们可以使用sublevels cut向量的factor中提取数值,转换为numeric并获取{{1}来自矢量('first'和'last')的{}}和min

max

数据

first <- as.numeric(sub("(\\(|\\[)([^,]+),.*", "\\2", levels(v1)))
last <- as.numeric(sub(".*,([^]]+)\\]", "\\1", levels(v1)))
min(first)
max(last)