New line in r

In R, there are a few ways to create a new line:

  1. Using the print() function: You can use the print() function with a newline character (\n) at the end of the string.

    print("Hello, world!\n")

    This will print "Hello, world!" followed by a new line.

  2. Using the cat() function: The cat() function is similar to print(), but it allows you to specify a separator between elements.

    cat("Hello, world!\n")

    This will also print "Hello, world!" followed by a new line.

  3. Using the paste() function: The paste() function can be used to concatenate strings with a newline character in between.

    paste("Hello, world!", "\n")

    This will create a string with "Hello, world!" followed by a newline character.

  4. Using the strwrap() function: The strwrap() function is part of the utils package and can be used to wrap a string to a specified width, including inserting newline characters.

    library(utils)
    strwrap("Hello, world!", width = 10)

    This will wrap the string "Hello, world!" to a width of 10 characters, inserting newline characters as needed.

  5. Using the writeLines() function: The writeLines() function is part of the base package and can be used to write a character vector to a file or to the console, with each element separated by a newline character.

    writeLines(c("Hello, world!", "This is a new line."))

    This will write "Hello, world!" followed by a new line, and then "This is a new line." to the console.