R 只重复一个元素并改变其他元素

R repeat only one element and change other elements

我需要这个:

"X","Y",151,"X","Y",152,"X","Y",153,"X","Y",154,....."X","Y",334

但我只能得到这个:

names<-c("X","Y",seq(152,334,by=1))
#"X","Y",151,152,153,154....334

谢谢

我们创建一个序列向量 ('v1'),通过 'v1' 和 replace thelength 复制向量 (c("X", "Y", ""))"" ` 与 'v1'.

v1 <-  152:334
v2 <- rep(c("X", "Y", ""), length(v1))
v3 <- replace(v2, v2 == "", v1)
head(v3, 10)
#[1] "X"   "Y"   "152" "X"   "Y"   "153" "X"   "Y"   "154" "X"  
tail(v3, 10)
# [1] "331" "X"   "Y"   "332" "X"   "Y"   "333" "X"   "Y"   "334"

或者另一种选择是通过 'v1' 的 lengthrbind 和 'v1' 复制字符串 ("X Y"),连接 (c) 矩阵到 vector, scan 拆分 "X Y""X", "Y".

scan(text=c(rbind(rep("X Y", length(v1)),  v1)), what = "", quiet=TRUE)