【问题标题】:extract values between special characters using R使用R提取特殊字符之间的值
【发布时间】:2015-03-11 20:23:24
【问题描述】:

我想提取[, 之间的值并将这些提取的值放入一个新列(col2)。

我不反对使用stringr 代替base。

示例数据:

df <- structure(list(t = structure(1:2, .Label = c("v1", "v2"), class = "factor"), 
    d = structure(1:2, .Label = c("something[123,894]", "something[456,4834]"
    ), class = "factor")), .Names = c("t", "d"), row.names = c(NA, 
-2L), class = "data.frame")

看起来像:

   t                   d
1 v1  something[123,894]
2 v2 something[456,4834]

现在我想创建一个新列 (df$r) 并将 123 的值提取到 v1456v2df$r

我确信有一种简单的方法可以使用正则表达式搜索[,,但我不适合使用regex

感谢您的帮助。

-樱桃树

【问题讨论】:

  • 啊,你只想要第一个字符串?我都在做。我认为一个更容易df$r &lt;- gsub('.*\\[(\\d{3}).*', '\\1', df$d)
  • @rawr,这是一个很好的解决方案,但我想知道 OP 是否总是在括号内的第一个数字中始终包含 3 位数字。
  • 好问题@DavidArenburg,它并不总是有 3 位数字......它可以从 1 到 4 位不等。
  • @cherrytree 然后将{3} 替换为+ :)
  • @rawr,我想你应该把它贴出来

标签: regex r


【解决方案1】:
df <- structure(list(t = structure(1:2, .Label = c("v1", "v2"), class = "factor"), 
                     d = structure(1:2, .Label = c("something[123,894]", "something[456,4834]"
                     ), class = "factor")), .Names = c("t", "d"), row.names = c(NA, 
                                                                                -2L), class = "data.frame")

这将匹配任意字符.*[ 任意次数,然后捕获到组\\1 一位或多位数字\\d+,结束捕获组,后跟任意次数任意字符

df$r <- gsub('.*\\[(\\d+).*', '\\1', df$d)

#    t                   d   r
# 1 v1  something[123,894] 123
# 2 v2 something[456,4834] 456

另外,如果你想捕获逗号后的第二个数字字符串,这会更有用:

gsub('.*\\[(\\d+),(\\d+).*', '\\1', df$d)
# [1] "123" "456"
gsub('.*\\[(\\d+),(\\d+).*', '\\2', df$d)
# [1] "894"  "4834"

或者如果你想同时做这两个:

cbind(df, do.call('rbind', lapply(strsplit(as.character(df$d), ','),
                                  function(x) gsub('\\D', '', x))))

#    t                   d   1    2
# 1 v1  something[123,894] 123  894
# 2 v2 something[456,4834] 456 4834

This 解释得比我好:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \[                       '['
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  ,                        ','
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))

【讨论】:

    猜你喜欢
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2017-09-02
    • 1970-01-01
    • 2021-07-18
    • 2019-01-28
    相关资源
    最近更新 更多