简体   繁体   English

将两个向量连接到一个新的向量

[英]Concatenating Two Vectors to a New Vector

I am trying to combine two vectors to create a new vector. 我试图结合两个向量来创建一个新的向量。 I tried this (well, something similar to this). 我试过这个(好吧,类似的东西)。

y <- c(10,10,11,11)
m <- c(1,2,1,2)
my <- data.frame(m,y)
paste(my[1], my[2], sep = "")

I get this: 我明白了:

# [1] "c(1, 2, 1, 2)c(10, 10, 11, 11)"`

I get the same thing when I use collapse instead of sep . 当我使用collapse而不是sep时,我得到同样的东西。

I am trying to get: 我想得到:

# ["1 10","2 10","1 11","2 11"]

Try any of the following instead: 请尝试以下任何一种方法:

paste(my[[1]], my[[2]])
# [1] "1 10" "2 10" "1 11" "2 11"

paste(my[, 1], my[, 2])
# [1] "1 10" "2 10" "1 11" "2 11"

paste(my$m, my$y)
# [1] "1 10" "2 10" "1 11" "2 11"

do.call(paste, my)
# [1] "1 10" "2 10" "1 11" "2 11"

The problem is that you weren't actually selecting the values in that column, but rather, a single-column data.frame . 问题是您实际上并没有选择该列中的值,而是选择单列data.frame

Of course, you could just use your original vectors: 当然,您可以使用原始向量:

> paste(m, y)
[1] "1 10" "2 10" "1 11" "2 11"

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

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