【问题标题】:R: Paste together some string vector elements based on list of indexesR:根据索引列表将一些字符串向量元素粘贴在一起
【发布时间】:2015-11-27 02:41:14
【问题描述】:

我有一个这样的字符串向量:

x <- c("ermanaric cayce nonwashable climactical outseeing dorr nubble",
       "aver unsegregating preprofess lumme noontime triskele",
       "riverbank walachian penza",
       "schlieren calthrop",
       "hutlike paraphyllium unservile chaplainship bordelaise",
       "phlogotic strategics jowlier orthopaedic nonprofiteering",
       "vizir rudenture shopkeeper",
       "interestuarine sardis",
       "anthas figuring",
       "unphased engle german emporium organometallic didy uneclipsing",
       "bronzy conant reballot",
       "extrados facinorous acrolithic",
       "paralyzation uningratiating enzymatically enuresis",
       "unscholastic extemporarily",
       "discipleship fossilize summae",
       "concretize intercharge palpate gombroon initiatrices",
       "intimation progressiveness",
       "unpictorialise",
       "romanticization",
       "wynnewood",
       "unmate libratory polysynthetic")

有些元素需要粘贴在一起形成更长的字符串。我有一个向量列表,其中包含必须粘贴在一起的元素的索引:

indx <- list(c(3, 4), c(7, 8, 9), c(11, 12), c(14, 15), c(17, 18, 19, 20))

也就是说,第3个和第4个元素必须粘贴在一起形成字符串"riverbank walachian penza schlieren calthrop",第7、8和9个字符串要粘贴在一起形成字符串"vizir rudenture shopkeeper interestuarine sardis anthas figuring"等等(编辑),保持其余字符串的顺序相同。生成的字符串向量如下所示:

y <- c("ermanaric cayce nonwashable climactical outseeing dorr nubble",
       "aver unsegregating preprofess lumme noontime triskele",
       "riverbank walachian penza schlieren calthrop",
       "hutlike paraphyllium unservile chaplainship bordelaise",
       "phlogotic strategics jowlier orthopaedic nonprofiteering",
       "vizir rudenture shopkeeper interestuarine sardis anthas figuring",
       "unphased engle german emporium organometallic didy uneclipsing",
       "bronzy conant reballot extrados facinorous acrolithic",
       "paralyzation uningratiating enzymatically enuresis",
       "unscholastic extemporarily discipleship fossilize summae",
       "concretize intercharge palpate gombroon initiatrices",
       "intimation progressiveness unpictorialise romanticization wynnewood",
       "unmate libratory polysynthetic")

我尝试了以下方法但没有成功:

myfun <- function(obj, indx) {
  paste(obj)[length(indx)]
}

mapply(myfun, x, m)

有人可以帮忙吗?

【问题讨论】:

    标签: r string vector indexing


    【解决方案1】:

    indx 不包含x 中每个项目的条目,但您希望每个项目返回或合并到某处,这一事实使得这更具挑战性。

    一个想法是使用Reduce 连续更新,使用临时NAs 来维护与索引号的对应关系。

    my.y<-c(na.omit(Reduce(function(s,i) 
      replace(s,i,c(paste(s[i],collapse=" "),rep(NA,length(i)-1))),indx,x)))
    
    identical(my.y,y)
    #> [1] TRUE
    

    匹配所需的输出。

    【讨论】:

    • 像魅力一样工作!非常感谢您,还有关于Reduce的提示。
    【解决方案2】:
    indx <- list(c(3, 4), c(7, 8, 9), c(11, 12), c(14, 15), c(17, 18, 19, 20))
    indx1 <- c(lapply(setdiff(1:length(x),unlist(indx)),c),indx)
    
    indx2 <- indx1[order(sapply(indx1,"[[",1))]
    
    sapply(indx2,function(z) {paste(x[z],collapse = " ")})
    

    【讨论】:

    • 非常感谢。这完成了粘贴索引元素的工作,但不返回向量的其余部分。否则效果很好。
    • 从问题中并不清楚是否还需要其余的并按顺序排列。它现在固定了。它与发布的其他答案非常相似。
    • 你说得对,我没这么说。我修好了它。谢谢!
    【解决方案3】:
    indx2 <- sapply(indx, '[', 1)
    x[indx2] <- Map(function(x,y) paste(x[y], collapse=" "), list(x), indx)
    unlist(x[sort(c(setdiff(seq_along(x), unlist(indx)),indx2))])
    #  [1] "ermanaric cayce nonwashable climactical outseeing dorr nubble"      
    #  [2] "aver unsegregating preprofess lumme noontime triskele"              
    #  [3] "riverbank walachian penza schlieren calthrop"                       
    #  [4] "hutlike paraphyllium unservile chaplainship bordelaise"             
    #  [5] "phlogotic strategics jowlier orthopaedic nonprofiteering"           
    #  [6] "vizir rudenture shopkeeper interestuarine sardis anthas figuring"   
    #  [7] "unphased engle german emporium organometallic didy uneclipsing"     
    #  [8] "bronzy conant reballot extrados facinorous acrolithic"              
    #  [9] "paralyzation uningratiating enzymatically enuresis"                 
    # [10] "unscholastic extemporarily discipleship fossilize summae"           
    # [11] "concretize intercharge palpate gombroon initiatrices"               
    # [12] "intimation progressiveness unpictorialise romanticization wynnewood"
    # [13] "unmate libratory polysynthetic"
    

    【讨论】:

    • 很好,但这不会返回不应与任何其他项目合并的 x 项目
    • 粘贴的字符串最重要,即原始排序丢失。否则完美无缺。谢谢。
    • @panman 我不确定您的意思,输出与所需的输出完全相同。没有?
    • @Pierre:对不起,我把它搞砸了,清理了工作区并再次运行它。完美运行,谢谢。
    猜你喜欢
    • 2022-10-20
    • 2023-01-10
    • 2021-07-28
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 2020-01-07
    相关资源
    最近更新 更多