【问题标题】:Pasting elements of two vectors alphabetically按字母顺序粘贴两个向量的元素
【发布时间】:2014-10-24 15:43:00
【问题描述】:

假设我有两个向量:

a <- c("george", "harry", "harry", "chris", "steve", "steve", "steve", "harry")
b <- c("harry", "steve", "chris", "harry", "harry", "george", "chris", "george")

我想要做的是将第一对、第二对等粘贴在一起......但是,我想按字母顺序粘贴每对的两个元素。在上面的示例中,前 2 对已经按字母顺序排列,但第 3 对 'harry' 和 'chris' 不是。我想为这对返回“chris harry”。

我已经想出了如何分两步执行此操作,但想知道是否有一种仅使用 paste 的快速方法(单行方式)来执行此操作?

我的解决方案:

x <- apply(mapply(c, a, b, USE.NAMES = FALSE), 2, sort)
paste(x[1,], x[2,])

它按字母顺序给出对...但是有 1 行方式吗?

[1] "george harry" "harry steve"  "chris harry"  "chris harry"  "harry steve"  "george steve" "chris steve"  "george harry"

【问题讨论】:

    标签: r apply paste mapply


    【解决方案1】:

    这是一种方法:

    apply(cbind(a, b), 1, function(x) paste(sort(x), collapse=" "))
    
    ## [1] "george harry" "harry steve"  "chris harry"  "chris harry"  
    ## [5] "harry steve" "george steve" "chris steve"  "george harry"
    

    使用您的初始尝试,您也可以执行以下操作,但它们都需要更多输入(不确定速度):

    unlist(Map(function(x, y) paste(sort(c(x, y)), collapse=" "), a, b),,FALSE)
    mapply(function(x, y) paste(sort(c(x, y)), collapse=" "), a, b, USE.NAMES = FALSE)
    

    【讨论】:

    • 非常感谢。我不知道如何将排序合并到一个衬里
    【解决方案2】:

    这里有一个与 Tyler 类似的方法,但使用的是Map。从技术上讲,它是一个单行...

    unlist(Map(function(x,y) {
        paste(sort(c(x,y)), collapse = " ")
        }, a, b, USE.NAMES = FALSE))
    # [1] "george harry" "harry steve"  "chris harry"  "chris harry" 
    # [5] "harry steve"  "george steve" "chris steve"  "george harry"
    

    【讨论】:

      【解决方案3】:

      您自己的代码中的一个衬里:

      apply(data.frame(apply(mapply(c, a, b, USE.NAMES = FALSE),1,paste)),1,function(x) paste(x[1],x[2]))
      [1] "george harry" "harry steve"  "harry chris"  "chris harry"  "steve harry"  "steve george" "steve chris"  "harry george"
      
      
      apply(apply(mapply(c, a, b, USE.NAMES = FALSE),2,sort),1,paste)
      
           [,1]     [,2]   
      [1,] "george" "harry"
      [2,] "harry"  "steve"
      [3,] "chris"  "harry"
      [4,] "chris"  "harry"
      [5,] "harry"  "steve"
      [6,] "george" "steve"
      [7,] "chris"  "steve"
      [8,] "george" "harry"
      

      【讨论】:

      • 不错。你知道这种方法是否会比泰勒的方法更快/更慢吗?我问,因为我配对的向量包含 >200,000 个元素并且速度很重要
      • 您可以使用 system.time() 来测试哪个是最快的。知道结果会很有趣,因为已经发布了许多解决方案。
      【解决方案4】:

      有点多余,因为它排序了两次,但是矢量化了,

      paste(pmin(a,b), pmax(a,b))
      

      编辑:替换为ifelse

      ifelse(a < b, paste(a, b), paste(b, a))
      

      【讨论】:

      • 可能存在细微问题,具体取决于locale 设置等。
      • 我做了一个速度比较——这个方法非常快!
      【解决方案5】:

      这里是以上答案的速度对比...

      我从我自己的数据集中获取了在足球联赛前 4 级联赛中进行的所有英格兰足球比赛的数据,可在此处获取:https://github.com/jalapic/engsoccerdata

      数据集是“engsoccerdata”,我使用第 3 列和第 4 列(主队和客队)粘贴在一起。我将每一列转换为一个字符向量。每个向量都有 188,060 个元素 - 从 1888 年到 2014 年,英格兰足球排名前 4 位的足球比赛有 188,060 场。

      对比如下:

      df<-engsoccerdata
      
      a<-as.character(df[,3])
      b<-as.character(df[,4])
      
      #tyler1
      system.time(apply(cbind(a, b), 1, function(x) paste(sort(x), collapse=" ")))
      
      #tyler2
      unlist(Map(function(x, y) paste(sort(c(x, y)), collapse=" "), a, b),,FALSE)
      
      #tyler3
      mapply(function(x, y) paste(sort(c(x, y)), collapse=" "), a, b, USE.NAMES = FALSE)
      
      #baptiste1
      paste(pmin(a,b), pmax(a,b))
      
      #baptiste2
      ifelse(a < b, paste(a, b), paste(b, a))  
      
      #RichardS
      unlist(Map(function(x,y) {
        paste(sort(c(x,y)), collapse = " ")
      }, a, b, USE.NAMES = FALSE))
      
      
      #rnso1
      apply(data.frame(apply(mapply(c, a, b, USE.NAMES = FALSE),1,paste)),1,function(x) paste(x[1],x[2]))
      
      #rnso2
      apply(apply(mapply(c, a, b, USE.NAMES = FALSE),2,sort),1,paste) 
      

      system.time() 结果:

      #              user  system elapsed 
      #tyler1       42.92    0.02   43.73 
      #tyler2       14.68    0.03   15.04
      #tyler3       14.78    0.00   14.88 
      #baptiste1     0.79    0.00    0.84 
      #baptiste2     1.25    0.00    1.28 
      #RichardS     15.40    0.01   15.64
      #rnso1         6.22    0.10    6.41
      #rnso2        13.07    0.00   13.15 
      

      非常有趣。 baptiste 的方法快如闪电!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-04
        • 1970-01-01
        • 2021-12-12
        • 2013-10-16
        相关资源
        最近更新 更多