New line in r
In R, there are a few ways to create a new line:
-
Using the
print()
function: You can use theprint()
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.
-
Using the
cat()
function: Thecat()
function is similar toprint()
, 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.
-
Using the
paste()
function: Thepaste()
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.
-
Using the
strwrap()
function: Thestrwrap()
function is part of theutils
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.
-
Using the
writeLines()
function: ThewriteLines()
function is part of thebase
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.