我有一个像这样的字符串向量:
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)
有人可以帮忙吗?
答案 0 :(得分:4)
事件indx
不包含x
中每个项目的条目,但您希望每个项目都返回或合并到某处,这使得这更具挑战性。
一个想法是使用Reduce
连续更新,使用临时NA
来维护与索引号的对应关系。
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
匹配所需的输出。
答案 1 :(得分:2)
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"
答案 2 :(得分: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 = " ")})