英文:
Best way to programatically extend and re-order array in R?
问题
使用paste
函数可以得到cols3
,而不需要使用循环。以下是代码示例:
cols3 <- paste(cols1, cols2)
cols3
这将生成与所需cols3
相同的输出。
英文:
Very simple R question:
What simplest operation should I use with cols1
and cols2
to obtain cols3
below?
(using loop is not the answer)
> cols1 <- paste0(letters[1:3]);cols1
[1] "a" "b" "c"
> cols2 <- paste0(cols1, ".new"); cols2
[1] "a.new" "b.new" "c.new"
> cols3 = c("a", "a.new" , "b" ,"b.new" , "c", "c.new"); cols3
[1] "a" "a.new" "b" "b.new" "c" "c.new"
</details>
# 答案1
**得分**: 3
使用`c` + `rbind`:
```r
c(rbind(cols1, cols2))
#[1] "a" "a.new" "b" "b.new" "c" "c.new"
FYI,这有时被称为“交错”或“交织”。还有内置的vctrs::vec_interleave
:
vctrs::vec_interleave(cols1, cols2)
#[1] "a" "a.new" "b" "b.new" "c" "c.new"
英文:
With c
+ rbind
c(rbind(cols1, cols2))
#[1] "a" "a.new" "b" "b.new" "c" "c.new"
FYI, this is sometimes called "interleaving", or "interlacing".
There is also the built-in vctrs::vec_interleave
:
vctrs::vec_interleave(cols1, cols2)
#[1] "a" "a.new" "b" "b.new" "c" "c.new"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论