Turning numbers into strings

String Manipulation with stringr in R

Charlotte Wickham

Assistant Professor at Oregon State University

Turning a number into a string

estimate <- 1.34019029100

$1.34

as.character(estimate)
"1.340190291"
format(estimate, digits = 3)
"1.34"
formatC(estimate, 
        format = "f", digits = 2)
"1.34"
String Manipulation with stringr in R

Fixed and scientific formats

  • Fixed: decimal point between ones and tenths

  • Scientific: decimal point after first digit

chap_1_2.010.png

String Manipulation with stringr in R

Fixed and scientific formats

  • Fixed: decimal point between ones and tenths

  • Scientific: decimal point after first digit

chap_1_2.011.png

String Manipulation with stringr in R

Fixed and scientific formats

  • Fixed: decimal point between ones and tenths

  • Scientific: decimal point after first digit

chap_1_2.012.png

String Manipulation with stringr in R

Fixed and scientific formats

  • Fixed: decimal point between ones and tenths

  • Scientific: decimal point after first digit

chap_1_2.013.png

String Manipulation with stringr in R

Fixed and scientific formats

chap_1_2.015.png

String Manipulation with stringr in R

Fixed and scientific formats

chap_1_2.016.png

String Manipulation with stringr in R

Fixed and scientific formats

chap_1_2.017.png

1989000000000000000000000000000
1.989e+30
0.000000000008
8e-12
String Manipulation with stringr in R

format() and formatC()

x <- c(1989000000000000000000000000000, 0.000000000008)
format(x, scientific = TRUE)
"1.989e+30" "8.000e-12"
format(x, scientific = FALSE)
"1988999999999999901909255192576.000000000000"
"                              0.000000000008"
String Manipulation with stringr in R

format() and formatC()

formatC(x, format = "f")
"1988999999999999901909255192576.0000"
"0.0000"
formatC(x, format = "e")
"1.9890e+30" "8.0000e-12"
formatC(x, format = "g")
"1.989e+30" "8e-12"
String Manipulation with stringr in R

Let's practice!

String Manipulation with stringr in R

Preparing Video For Download...