【问题标题】:Regular Expressions to Unmerge row entries用于取消合并行条目的正则表达式
【发布时间】:2018-10-12 19:56:56
【问题描述】:

我有一个由

给出的示例数据集
df <- data.frame(
country = c("GermanyBerlin", "England (UK)London", "SpainMadrid", "United States of AmericaWashington DC", "HaitiPort-au-Prince", "country66city"),
  capital = c("#Berlin", "NA", "#Madrid", "NA", "NA", "NA"),
  url = c("/country/germany/01", "/country/england-uk/02", "/country/spain/03", "country/united-states-of-america/04", "country/haiti/05", "country/country6/06"),
  stringsAsFactors = FALSE
)

                                country capital                                 url
1                         GermanyBerlin #Berlin                 /country/germany/01
2                    England (UK)London      NA              /country/england-uk/02
3                           SpainMadrid #Madrid                   /country/spain/03
4 United States of AmericaWashington DC      NA country/united-states-of-america/04
5                   HaitiPort-au-Prince      NA                    country/haiti/05
6                         country66city      NA                 country/country6/06

我们的目的是整理这些列,使列与人们对它们的名称所期望的一样:

  • 第一个应该只包含国家名称。
  • 第二个应该包含大写字母(不带 # 符号)。
  • 第三个应该保持不变。

所以我想要的输出是:

                  country        capital                                 url
1                  Germany         Berlin                 /country/germany/01
2             England (UK)         London              /country/england-uk/02
3                    Spain         Madrid                   /country/spain/03
4 United States of America  Washington DC country/united-states-of-america/04
5                    Haiti Port-au-Prince                    country/haiti/05
6                 country6          6city                 country/country6/06

如果capital 列中有非 NA 条目,我有一段代码可以实现这一点(见帖子底部)。

因此,我正在寻找一种解决方案,该解决方案可以识别url 列的模式可用于将大写从country 列中拆分出来。

这需要考虑到这样一个事实

  • URL 文本全部小写,而出现在 country 列中的国家/地区名称大小写混合。
  • URL 中的文本用连字符替换空格。
  • url 删除了特殊字符(例如 UK 周围的括号)。

我很想看看这个目标是如何实现的,大概是使用正则表达式(尽管可以选择任何选项)。


capital 列非 NA 时的部分解决方案

如果capital 列中有非 NA 条目,则以下代码实现了我的目标:

df %>% mutate( capital =   str_replace(capital, "#", ""), 
               country = str_replace(country, capital,"") 
              )

                                country capital                                 url
1                               Germany  Berlin                 /country/germany/01
2                    England (UK)London      NA              /country/england-uk/02
3                                 Spain  Madrid                   /country/spain/03
4 United States of AmericaWashington DC      NA country/united-states-of-america/04

【问题讨论】:

    标签: r regex split pattern-matching


    【解决方案1】:

    您可以从这样的事情开始并继续改进,直到获得 (100%) 正确的结果,然后看看您是否可以跳过/合并任何步骤。

    library(magrittr)
    
    df$country2 <- df$url %>%
      gsub("-", " ", .) %>%
      gsub(".+try/(.+)/.+", "\\1", .) %>%
      gsub("(\\b[a-z])", "\\U\\1", ., perl = TRUE)
    
    df$capital <- df$country %>%
      gsub("[()]", " ", .) %>%
      gsub(" +", " ", .) %>%
      gsub(paste(df$country2, collapse = "|"), "", ., ignore.case = TRUE)
    
    df$country <- df$country2
    df$country2 <- NULL
    
    df
                       country        capital                                 url
    1                  Germany         Berlin                 /country/germany/01
    2               England Uk         London              /country/england-uk/02
    3                    Spain         Madrid                   /country/spain/03
    4 United States Of America  Washington DC country/united-states-of-america/04
    5                    Haiti Port-au-Prince                    country/haiti/05
    6                 Country6          6city                 country/country6/0
    

    【讨论】:

      【解决方案2】:

      你可以的

      transform(df,capital=sub(".*[A-Z]\\S+([A-Z])","\\1",country))
      
                                     country       capital                                 url
      1                         GermanyBerlin        Berlin                 /country/germany/01
      2                    England (UK)London        London              /country/england-uk/02
      3                           SpainMadrid        Madrid                   /country/spain/03
      4 United States of AmericaWashington DC Washington DC country/united-states-of-america/04
      

      【讨论】:

      • 感谢您的回复;感谢您的解决方案避免使用 url 列,但不幸的是,您的解决方案不够灵活,无法在列中包含可能条目的选项。例如,它目前无法处理大写名称本身中的连字符/特殊字符的情况。此外(可能有点不切实际,但在我的实际应用中很重要)国家或首都可能存在数字的可能性。我已经扩展了我的基本示例以涵盖这些。
      • 我认为使用 URL 列应该有助于发现数字周围的任何奇怪行为(在最后一个示例中,URL 列选择 6 中的一个属于国家,而另一个属于首都)。在我的场景中,一个合理的假设是大写列中的一个预期条目永远不会以特殊字符开头(但可以以数字开头)。再次感谢您的想法!感谢您的解决方案在我最初提供的数据上运行良好,但不幸的是,我很难将所有潜在的陷阱放入示例“单元测试”中!
      • 如果城市名称以大写字母开头,则在所有情况下都可以使用..无论前面是什么..无论是否是 nu,bers..唯一的例外是如果有空格那么它就不会识别..但是只要城市和国家名称在一起并且它们都以大写字母开头,那么我们就会得到城市名称
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 2014-10-20
      • 2014-07-26
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多