【问题标题】:Merging 2 vectors and removing all repetitions合并 2 个向量并删除所有重复项
【发布时间】:2017-02-03 05:41:24
【问题描述】:

假设有一个向量:

v1 <- c("ab", "bc", "cd", "ef", "yolo", "da", "sm", "ez-de") 
v2 <- c("ab", "bc", "cd", "ef", "yolo-da", "sm", "ez", "de")

如何合并上面的两个向量,得到下面的?

c("ab", "bc", "cd", "ef", "yolo-da", "sm", "ez-de") 

请注意,上面的两个向量具有相同的长度..

【问题讨论】:

  • 你能展示其他模式吗?
  • 两个向量的长度总是相同的。您始终使用破折号保留元素,并且只使用一次重复的元素。任何没有破折号的元素总是两个向量中的重复元素
  • c("lol", "it","is", "now", "jab-time").和 c("lol", "it-is", "now", "jab", "time")。合并到 c("lol", "it-is", "now", "jab-time")。一个向量 js 中的最后一个元素始终是虚线元素。

标签: r


【解决方案1】:

如果值的顺序不是问题,我们可以试试这个:

v <- union(v1, v2)
Filter(function(x) length(grep(x, v))==1, v)
# [1] "ab"      "bc"      "cd"      "ef"      "sm"      "ez-de"   "yolo-da"

【讨论】:

    【解决方案2】:

    逐步解决方案;理解后可以减少步骤

    # case 1. 
    a=c("ab", "bc", "cd", "ef", "yolo", "da", "sm", "ez-de") 
    b=c("ab", "bc", "cd", "ef", "yolo-da", "sm", "ez", "de")
    # [1] "ab"      "bc"      "cd"      "ef"      "sm"      "yolo-da" "ez-de" 
    
    # case 2.
    a = c("lol", "it","is", "now", " jab-time")
    b = c("lol", "it-is", " now", "jab", " time")
    # [1] "lol"      "now"      "it-is"    "jab-time"
    
    a = trimws(a)  # since observed that case 2 . "now" had whitespaces
    b = trimws(b)  # these 2 steps are unnecessary, just check if that was a typo
    
    c = intersect(a, b)  # extract the common values from both vectors
    a = a[!(a %in% c)]   # keep only those which are not there in c
    b = b[!(b %in% c)]   # keep only those which are not there in c
    
    d = grep("-", c(a, b), value = TRUE)  # this returns only those having "-" in it
    
    ans <- c(c , d)   
    

    【讨论】:

      【解决方案3】:

      我的方法:

      library(dplyr)
      
      a=c("ab", "bc", "cd", "ef", "yolo", "da", "sm", "ez-de") 
      b=c("ab", "bc", "cd", "ef", "yolo-da", "sm", "ez", "de")
      
      ab <- c(a,b)
      ab_unique <- unique(ab)
      
      ab
      ab_unique
      

      【讨论】:

        猜你喜欢
        • 2015-06-09
        • 2020-05-20
        • 1970-01-01
        • 2021-12-03
        • 2016-10-24
        • 2018-03-11
        • 1970-01-01
        • 2021-06-06
        • 1970-01-01
        相关资源
        最近更新 更多