r - 比较 dplyr 中的字符串

标签 r dplyr tidyverse

我的数据框看起来像:

df <- tibble::tribble(
  ~order_id, ~user_id,      ~comp_order,           ~comp_rec,
    1164320,    32924, "4-6-22-11-37-5", "4-5-6-11-22-36-37",
    1169182,    33128,  "9-4-15-28-8-7",  "4-7-8-9-28-37-38",
    1166014,    33003,    "27-22-4-6-5", "4-5-6-22-27-36-37",
    1166019,    32996,    "27-22-4-6-8", "4-6-8-22-27-36-38"
  )

我想知道 comp_order 列中而不是 comp_rec 中存在的数字。

最终输出应该是这样的:

  order_id user_id comp_rec          comp_order     is_equal elements_removed_from_rec elements_added_to_order
     <dbl>   <dbl> <chr>             <chr>          <chr>    <chr>                     <chr>                  
1  1164320   32924 4-5-6-11-22-36-37 4-6-22-11-37-5 no       36                        none                   
2  1169182   33128 4-7-8-9-28-37-38  9-4-15-28-8-7  no       37                        none                   
3  1166014   33003 4-5-6-22-27-36-37 27-22-4-6-5    no       36-37                     none                   
4  1166019   32996 4-6-8-22-27-36-38 27-22-4-6-8    no       36-38                     none                   
5  1166012   32922 27-22-4-6-8       27-22-4-6-8    yes      none                      none                   
6  1166033   32911 27-22-4-6-8       27-22-4-6-8-33 no       none                      33                     

df_output <- tibble::tribble(
               ~order_id, ~user_id,           ~comp_rec,      ~comp_order, ~is_equal, ~elements_removed_from_rec, ~elements_added_to_order,
                 1164320,    32924, "4-5-6-11-22-36-37", "4-6-22-11-37-5",      "no",                       "36",                   "none",
                 1169182,    33128,  "4-7-8-9-28-37-38",  "9-4-15-28-8-7",      "no",                       "37",                   "none",
                 1166014,    33003, "4-5-6-22-27-36-37",    "27-22-4-6-5",      "no",                    "36-37",                   "none",
                 1166019,    32996, "4-6-8-22-27-36-38",    "27-22-4-6-8",      "no",                    "36-38",                   "none",
                 1166012,    32922,       "27-22-4-6-8",    "27-22-4-6-8",     "yes",                     "none",                   "none",
                 1166033,    32911,       "27-22-4-6-8", "27-22-4-6-8-33",      "no",                     "none",                     "33"
               )

我需要知道:

  1. 已从 rec 中删除的内容
  2. 已添加到订单中的内容

根据字符串中的数字。

字符串中数字顺序不一定相同的问题...

如何比较这两个字符串?

最佳答案

这是使用 purrr 的方法:

首先,我们使用purrr:map- 上的元素拆分为一个元素列表。然后,我们使用 purrr:map2 对列表执行 setdiff 以识别不同的元素。

如果两者的计算结果都为 "",那么我们知道它们是相同的,因此我们可以使用 case_when 来确定 is_equal 列.

然后我们可以通过删除列表列来清理。

library(dplyr)
library(purrr)
df %>%
  mutate(comp_order_list = map(comp_order, ~str_split(.,"-", simplify = TRUE)),
         comp_rec_list = map(comp_rec, ~str_split(.,"-", simplify = TRUE)),
         elements_removed = map2_chr(comp_rec_list,comp_order_list,
                                   ~ paste(setdiff(.x,.y),collapse = "-")),
         elements_added = map2_chr(comp_order_list,comp_rec_list,
                                     ~ paste(setdiff(.x,.y),collapse = "-")),
         is_equal = case_when(elements_removed == "" & elements_added == "" ~ "yes",
                              TRUE ~ "no")) %>%
  dplyr::select(order_id,user_id,comp_rec,comp_order,is_equal,elements_removed,elements_added)
# A tibble: 6 x 7
  order_id user_id comp_rec          comp_order     is_equal elements_removed elements_added
     <dbl>   <dbl> <chr>             <chr>          <chr>    <chr>            <chr>         
1  1164320   32924 4-5-6-11-22-36-37 4-6-22-11-37-5 no       "36"             ""            
2  1169182   33128 4-7-8-9-28-37-38  9-4-15-28-8-7  no       "37-38"          "15"          
3  1166014   33003 4-5-6-22-27-36-37 27-22-4-6-5    no       "36-37"          ""            
4  1166019   32996 4-6-8-22-27-36-38 27-22-4-6-8    no       "36-38"          ""            
5  1166012   32922 27-22-4-6-8       27-22-4-6-8    yes      ""               ""            
6  1166033   32911 27-22-4-6-8       27-22-4-6-8-33 no       ""               "33"    

关于r - 比较 dplyr 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61958715/

相关文章:

r - 循环跨列相乘

r - 如何在 purrr::pmap 中 fork /并行化进程

r - 如何在整列的特定位置添加数值?

r - 使用 ggplot 包 R 更改图中 yaxis 面的标签

r - 使用 sf dplyr 在 R 中按组计算逐点距离

r - 在 Shiny 中结合 selectInput 和 DT::datatable 编辑

r - 寻找区间集之间的重叠/高效的重叠连接

r - 在 mutate_if 调用中提取列名

R extrafont 包,依赖项 ‘Rttf2pt1’ 不可用

R 语言函数工厂 : How to ensure safety?