Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:根据索引列表将一些字符串向量元素粘贴在一起_R_String_Vector_Indexing - Fatal编程技术网

R:根据索引列表将一些字符串向量元素粘贴在一起

R:根据索引列表将一些字符串向量元素粘贴在一起,r,string,vector,indexing,R,String,Vector,Indexing,我有这样一个字符串向量: x <- c("ermanaric cayce nonwashable climactical outseeing dorr nubble", "aver unsegregating preprofess lumme noontime triskele", "riverbank walachian penza", "schlieren calthrop", "hutlike paraphyllium unse

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

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")
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)

myfun
indx2
indx事实上,
indx
中的每个项目都不包含条目,但您希望每个项目都返回或合并到某个地方,这使得这一点更具挑战性

一个想法是使用
Reduce
连续更新,使用临时
NA
s来保持与索引号的对应关系

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
my.y[1]正确

匹配所需的输出。

Nice,但这不会返回不应与任何其他项合并的x项粘贴的字符串显示在所有其他项之上,即原始排序丢失。否则效果会很好。谢谢你。@panman我不知道你的意思,输出与期望的输出完全相同。没有?@Pierre:对不起,我把这里搞砸了,清理了工作区,然后又运行了一遍。非常好,谢谢。非常感谢。这会粘贴已编制索引的元素,但不会返回向量的其余部分。否则效果很好。从这个问题上看,剩下的也需要,而且顺序也不明显。现在修好了。这与贴出的另一个答案非常相似。你是对的,我没有这么说。我修好了。非常感谢。工作很有魅力!非常感谢您,也感谢您对
Reduce
的提示。
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"
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 = " ")})
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