在左侧关闭时间间隔提取断点

Sea*_*ado 2 regex cut r

我正在查看命令cut()(example(cut))的示例菜单,特别是这部分:

cut> aaa <- c(1,2,3,4,5,2,3,4,5,6,7)

cut> cut(aaa, 3)
[1] (0.994,3] (0.994,3] (3,5]     (3,5]     (3,5]     (0.994,3]
[7] (3,5]     (3,5]     (3,5]     (5,7.01]  (5,7.01] 
Levels: (0.994,3] (3,5] (5,7.01]

cut> cut(aaa, 3, dig.lab = 4, ordered = TRUE)
[1] (0.994,2.998] (0.994,2.998] (2.998,5.002] (2.998,5.002]
[5] (2.998,5.002] (0.994,2.998] (2.998,5.002] (2.998,5.002]
[9] (2.998,5.002] (5.002,7.006] (5.002,7.006]
Levels: (0.994,2.998] < (2.998,5.002] < (5.002,7.006]

cut> ## one way to extract the breakpoints
cut> labs <- levels(cut(aaa, 3))

cut> cbind(lower = as.numeric( sub("\\((.+),.*", "\\1", labs) ),
cut+       upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) ))
     lower upper
[1,] 0.994  3.00
[2,] 3.000  5.00
[3,] 5.000  7.01
Run Code Online (Sandbox Code Playgroud)

如果间隔在右边关闭(如上所示),那么它显示了一种使用提取数据断点的方法cbind()

现在,让我们假设我的数据将被剪切,但表示间隔在左侧关闭.

cut(aaa, 3, dig.lab = 4, ordered = TRUE, right = FALSE)
Run Code Online (Sandbox Code Playgroud)

如何使用相同的命令提取现在的断点cbind()?(如果有更多方法,欢迎你)

A5C*_*2T1 7

只需使用以下内容作为您的模式,然后使用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"
Run Code Online (Sandbox Code Playgroud)

而且,这是一种快速读取数据的方法:

read.csv(text = gsub("\\[|\\]|\\(|\\)", "", out), header = FALSE)
#      V1    V2
# 1 0.994 2.998
# 2 2.998 5.002
# 3 5.002 7.006
Run Code Online (Sandbox Code Playgroud)

仅供参考:无论区间是左侧还是右侧,都可以使用相同的模式.使用您的原始示例:

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
Run Code Online (Sandbox Code Playgroud)

至于替代方案,因为你只需要在使用前删除第一个和最后一个字符read.csv,你也可以轻松使用substr而不必对正则表达式大惊小怪(如果这不是你的事情):

substr(labs, 2, nchar(labs)-1)
# [1] "0.994,3" "3,5"     "5,7.01" 
Run Code Online (Sandbox Code Playgroud)

更新:一个完全不同的选择

由于很明显R必须计算这些值并将它们存储为函数的一部分以生成您看到的输出,因此操作函数以使其输出不同的东西并不太难.

查看代码cut.default,您会发现以下几行:

if (codes.only) 
    code
else factor(code, seq_along(labels), labels, ordered = ordered_result)
Run Code Online (Sandbox Code Playgroud)

更改最后几行非常容易输出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]))
Run Code Online (Sandbox Code Playgroud)

现在,比较:

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
Run Code Online (Sandbox Code Playgroud)

而且,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
Run Code Online (Sandbox Code Playgroud)