How to combine numbers into strings in R?

I have a data frame with numbers similar to:

df$X1    df$X2

.9     1.442

How can I use elements from this data frame to create labels for confidence intervals that will be a well-formatted string:

(0.90, 1.42)

I was lost somewhere in between paste, as.characterand c()nothing does not work: /

+3
source share
2 answers

How about something like:

paste("(",df$X1,",",df$X2,")",sep="")

In your example, it looks like you can round your numbers, but rounding is wrong. Do you want a certain number of digits after the decimal point?

In addition, "1" and "2" frowned like names in data.frame - see ?make.names

EDIT:

To round in such cases, I like to use formatC():

paste("(", formatC(df$X1, digits = 2, format = "f"), ",",
  formatC(df$X2, digits = 2, format = "f"), ")", sep = "")

.005, , 0.00.

+6

+1 @BenBarnes, 1 . ( ) formatC() round(df$X1, digits=2). round , formatC(). , .

+4

All Articles