0

I can't believe that I struggle so much to find how to merge together the column names of a data frame into one single character string.

Let's take the mtcars dataset.

                   mpg cyl disp  hp drat    wt  qsec vs
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0
Valiant           18.1   6  225 105 2.76 3.460 20.22  1

I just want a character string like this as an output: "mpg + cyl + disp + hp + drat + wt + qsec + vs"

Of course I tried something like this:

> paste(colnames(mtcars))

 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec"
 [8] "vs"   "am"   "gear" "carb"

And this is not what I want, I want all of them merged together

Any idea?

FrsLry
  • 252
  • 2
  • 15

1 Answers1

1
paste0(colnames(mtcars), collapse = " ")
#> [1] "mpg cyl disp hp drat wt qsec vs am gear carb"
eonurk
  • 447
  • 2
  • 11