Domanda

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.

I am trying to get:

# ["1 10","2 10","1 11","2 11"]
È stato utile?

Soluzione

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.

Of course, you could just use your original vectors:

> paste(m, y)
[1] "1 10" "2 10" "1 11" "2 11"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top