OneBite.Dev - Coding blog in a bite size

format a string in R

Code snippet on how to format a string in R

  x <- "Hello World"
  cat(format(x, width=20, justify="left"))

This code will format a string, “Hello World” and print the output. The first line assigns the string to a variable, x. The second line calls the cat() function which prints out the string as output. The format() function is used to modify and adjust the look of the string. In this example, the width argument is set to 20 and the justify argument to “left”. This will print out the string so that it fills 20 characters and aligns left. The output should look like “Hello World ” with three empty spaces at the end.

r