0

I have 3 vectors: state.name, state.abb, state,area. i want to concatenate them to one vector in which every index looks like that:

s[1]

[1] "AL - Alabama - 51609 Sq. Mile"

how can i merge them to look like this?

nghauran
  • 6,648
  • 2
  • 20
  • 29
  • Take a look at `?paste` – G5W Oct 30 '17 at 17:00
  • or look at `?sprintf` – G. Grothendieck Oct 30 '17 at 17:01
  • Or `?str_c` from package `stringr` using collapse=" - " – Cedric Oct 30 '17 at 17:05
  • I think this is element-wise concatenation, so the `collapse` argument is unnecessary (`sep = ' - '` would be useful) and that's not the correct duplicate. – Gregor Thomas Oct 30 '17 at 17:09
  • but I need to create a vector before I use paste. Every index of this vector should consist the three these vector as a string. As you can see the first index of the "s" vector have a combination of these vectors. – Ron Milich Oct 30 '17 at 17:10
  • Use `paste`: `v1 <- letters[1:3] v2 <- letters[4:6] v3 <- letters[7:9] v <- paste(v1, v2, v3, sep = " - ") v[1]` – nghauran Oct 30 '17 at 17:11
  • You have vectors, right? `state.name`, `state.abb`, `state.area`? `paste` creates the vector. `s = paste(state.abb, state.name, state.area, sep = " - ")` – Gregor Thomas Oct 30 '17 at 17:12
  • Despite your vectors not being in a data frame (you could put them there...) I think this is the best dupe: [paste multiple columns together](https://stackoverflow.com/q/14568662/903061). – Gregor Thomas Oct 30 '17 at 17:13
  • And I think you are confused with the *index* of the vector and the *contents* of the vector. The *index* is **always a number**, starting with 1, going to the number of elements in the vector. The *contents* are the things (strings in this case) contained in each element. – Gregor Thomas Oct 30 '17 at 17:14
  • it works, thanks man:)) – Ron Milich Oct 30 '17 at 17:16

0 Answers0