【问题标题】:Extract unique string elements from dataframe column in R从R中的数据框列中提取唯一的字符串元素
【发布时间】:2020-07-02 16:18:56
【问题描述】:

我有一个包含两列的数据框:

  • id

  • string:这是一个文本字符串列,包含由符号/分隔的重复文本元素

    library(tidyverse)
    
    df_input <- data.frame(stringsAsFactors=FALSE,
                        id = c(123, 234, 345, 456),
                    string = c("[\"aaa\"] / [\"aaa\"] / [\"aaa\"] / bbb / bbb / bbb",
                               "[\"hello hello\"] / [\"hello hello\"] / [\"hello hello\"] / [\"hello hello\"]",
                               "my name is tim / my name is tim / my name is tim", "[\"hello word\"]")
              )
    

看起来像:

       id                                                                string
     1 123                         ["aaa"] / ["aaa"] / ["aaa"] / bbb / bbb / bbb
     2 234 ["hello hello"] / ["hello hello"] / ["hello hello"] / ["hello hello"]
     3 345                      my name is tim / my name is tim / my name is Tim
     4 456                                                        ["hello word"]

我看到的模式是每次有一组重复的元素,用符号/隔开:

["aaa"] / ["aaa"] / ["aaa"] / bbb / bbb / bbb

或者:

my name is tim / my name is tim / my name is Tim

但也有单个元素的情况:

["hello word"]

我想要一个如下所示的数据框:

df_output <- data.frame(stringsAsFactors=FALSE,
                       id = c(123, 234, 345, 456),
                   string = c("[\"aaa\"] / bbb", "[\"hello hello\"]", "my name is tim",
                              "[\"hello word\"]")
             )

地点:

   id          string
1 123   ["aaa"] / bbb
2 234 ["hello hello"]
3 345  my name is tim
4 456  ["hello word"]

我只保留独特的元素;如果存在多个元素,则以/ 分隔。

dplyr有什么解决办法吗?

【问题讨论】:

    标签: r dplyr tidyverse


    【解决方案1】:

    使用 stringr 的 str_split 和 purrr 到 map 的独特发现和重组:

    library(stringr)
    library(purrr)
    library(dplyr)
    
    df_input %>%
      mutate(string = string %>% 
               str_split(" / ") %>%
               map(unique) %>%
               map_chr(paste, collapse = " / "))
    #>    id          string
    #> 1 123   ["aaa"] / bbb
    #> 2 234 ["hello hello"]
    #> 3 345  my name is tim
    #> 4 456  ["hello word"]
    

    reprex package (v0.3.0) 于 2020 年 7 月 2 日创建

    【讨论】:

      【解决方案2】:

      您可以使用dplyrtidyr

      df_input %>%
        separate_rows(string, sep=" / ") %>%
        distinct() %>%
        group_by(id) %>%
        summarise(string = paste(string, collapse=" / "), .groups="drop") %>%
        as.data.frame()
      

      返回

         id          string
      1 123   ["aaa"] / bbb
      2 234 ["hello hello"]
      3 345  my name is tim
      4 456  ["hello word"]
      

      group_bysummariseas.data.frame 部分可以跳过

      aggregate(string ~ id, ., paste, collapse=" / ")
      

      【讨论】:

        【解决方案3】:

        cSplitdata.table 的选项

        library(splitstackshape)
        unique(cSplit(df_input, "string", sep= " / ", "long"))[,
               .(string = paste(string, collapse= " / ")),.(id)]
        #   id          string
        #1: 123   ["aaa"] / bbb
        #2: 234 ["hello hello"]
        #3: 345  my name is tim
        #4: 456  ["hello word"]
        

        【讨论】:

          猜你喜欢
          • 2013-06-05
          • 1970-01-01
          • 2020-06-21
          • 1970-01-01
          • 1970-01-01
          • 2021-12-03
          • 1970-01-01
          • 2016-07-25
          • 1970-01-01
          相关资源
          最近更新 更多