1

This is probably simple in R, but I cannot find a working solution. I have a list of numbers, i.e. c(1,2,3,4) and I want to get one single string " 1 2 3 4" where every number (can be float) have a fixed width.

> dput(l)
c(1, 1.5, 2, 2.5)
> paste(l)
[1] "1"   "1.5" "2"   "2.5"
> paste(as.character(l))
[1] "1"   "1.5" "2"   "2.5"
> str_pad(l,width=7,side="left")
[1] "      1" "    1.5" "      2" "    2.5"
> paste(str_pad(l,width=7,side="left"))
[1] "      1" "    1.5" "      2" "    2.5"

And many other tries, nothing work. I need result in the variable not just on the screen.

meolic
  • 1,177
  • 2
  • 15
  • 41
  • 2
    You're close - use the `collapse` argument of paste to make the result a single string. `paste(str_pad(l,width=7,side="left"), collapse = "")` – Gregor Thomas Sep 23 '19 at 19:59

0 Answers0