只需为您的模式使用以下内容,然后gsub
改用:"\\[|\\]|\\(|\\)"
.
一个例子。
out <- levels(cut(aaa, 3, dig.lab = 4, ordered = TRUE, right = FALSE))
gsub("\\[|\\]|\\(|\\)", "", out)
# [1] "0.994,2.998" "2.998,5.002" "5.002,7.006"
而且,这是读取该数据的快速方法:
read.csv(text = gsub("\\[|\\]|\\(|\\)", "", out), header = FALSE)
# V1 V2
# 1 0.994 2.998
# 2 2.998 5.002
# 3 5.002 7.006
仅供参考:无论间隔是在左侧还是右侧闭合,相同的模式都将起作用。使用您的原始示例:
labs <- levels(cut(aaa, 3))
labs
# [1] "(0.994,3]" "(3,5]" "(5,7.01]"
read.csv(text = gsub("\\[|\\]|\\(|\\)", "", labs), header = FALSE)
# V1 V2
# 1 0.994 3.00
# 2 3.000 5.00
# 3 5.000 7.01
至于替代方案,由于您只需要在使用 之前去掉第一个和最后一个字符,因此read.csv
您也可以轻松使用substr
,而不必对正则表达式大惊小怪(如果那不是你的事):
substr(labs, 2, nchar(labs)-1)
# [1] "0.994,3" "3,5" "5,7.01"
更新:一个完全不同的选择
由于很明显 R 必须计算这些值并将它们存储为函数的一部分才能生成您看到的输出,因此操作该函数以使其输出不同的东西并不难。
查看 的代码cut.default
,您会发现最后几行如下:
if (codes.only)
code
else factor(code, seq_along(labels), labels, ordered = ordered_result)
更改最后几行以输出 a 非常容易list
,其中包含cut
作为第一项的输出和计算的范围(cut
直接从函数中提取,而不是从粘贴在一起的factor
labels
.
例如,在我在此链接上发布的 Gist 中,我将这些行更改如下:
if (codes.only)
FIN <- code
else FIN <- factor(code, seq_along(labels), labels, ordered = ordered_result)
list(output = FIN, ranges = data.frame(lower = ch.br[-nb], upper = ch.br[-1L]))
现在,比较:
cut(aaa, 3)
# [1] (0.994,3] (0.994,3] (3,5] (3,5] (3,5] (0.994,3] (3,5] (3,5]
# [9] (3,5] (5,7.01] (5,7.01]
# Levels: (0.994,3] (3,5] (5,7.01]
CUT(aaa, 3)
# $output
# [1] (0.994,3] (0.994,3] (3,5] (3,5] (3,5] (0.994,3] (3,5] (3,5]
# [9] (3,5] (5,7.01] (5,7.01]
# Levels: (0.994,3] (3,5] (5,7.01]
#
# $ranges
# lower upper
# 1 0.994 3
# 2 3 5
# 3 5 7.01
并且,right = FALSE
:
cut(aaa, 3, dig.lab = 4, ordered = TRUE, right = FALSE)
# [1] [0.994,2.998) [0.994,2.998) [2.998,5.002) [2.998,5.002) [2.998,5.002)
# [6] [0.994,2.998) [2.998,5.002) [2.998,5.002) [2.998,5.002) [5.002,7.006)
# [11] [5.002,7.006)
# Levels: [0.994,2.998) < [2.998,5.002) < [5.002,7.006)
CUT(aaa, 3, dig.lab = 4, ordered = TRUE, right = FALSE)
# $output
# [1] [0.994,2.998) [0.994,2.998) [2.998,5.002) [2.998,5.002) [2.998,5.002)
# [6] [0.994,2.998) [2.998,5.002) [2.998,5.002) [2.998,5.002) [5.002,7.006)
# [11] [5.002,7.006)
# Levels: [0.994,2.998) < [2.998,5.002) < [5.002,7.006)
# $ranges
# lower upper
# 1 0.994 2.998
# 2 2.998 5.002
# 3 5.002 7.006