Reputation: 13616
I have two numerical vectors in R
c(1,2,3,4,5)
c(0.1,0.2,0.3,0.4,0.5)
I would like to convert them to strings, combine them, and apply formatting, producing one vector:
c("1 (10.0%)", "2 (20.0%)", "3 (30.0%)", "4 (40.0%)", "5 (50.0%)")
It's more important for the solution to be simple and comprehensible than performant in my case.
Upvotes: 1
Views: 239
Reputation: 11893
Here's a very simple implementation:
> num <- c(1,2,3,4,5)
> pct <- c(0.1,0.2,0.3,0.4,0.5)
>
> pctChar <- paste0("(", pct*100, "%", ")")
> char.out <- paste(num, pctChar)
> char.out
[1] "1 (10%)" "2 (20%)" "3 (30%)" "4 (40%)" "5 (50%)"
Note that paste0()
by default does not put spaces between the components, and that paste()
does.
Upvotes: 1
Reputation: 4995
x<-c(1,2,3,4,5)
y<-c(0.1,0.2,0.3,0.3,0.4,0.5)
y <- paste0(y * 100, "%")
result <- paste0(x," (", y, ")")
Upvotes: 2