简体   繁体   English

如何在R中的新向量中合并向量元素

[英]How to merge elements of vectors in new vector in R

I want to extract the elements of each vector and get new vector that attached the elements 我想提取每个向量的元素并获取附加了这些元素的新向量

x<-c(1,2,3,4)
y<-c(w,w,w,w)
z<-c(1,2,3,4)

Expected result 预期结果

xyz<-c(1w1,2w2,3w3,4w4)

Thank you 谢谢

I think a pretty straightforward paste0 will get it done here. 我认为非常简单的paste0可以在这里完成它。

> x <- 1:4; y <- rep("w", 4); z <- 1:4
> (xyz <- paste0(x, y, z))
# [1] "1w1" "2w2" "3w3" "4w4"

cat would also work, cat也可以

> cat(paste0(x, y, z), sep = ",")
# 1w1,2w2,3w3,4w4

Also, 也,

as.character(interaction(x,y,z,sep=""))
#[1] "1w1" "2w2" "3w3" "4w4"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM