Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - do I need to add explicit new line character with print()?

How do I use the new line character in R?

myStringVariable <- "Very Nice ! I like";  myStringVariabel <- paste(myStringVariable, "\n", sep=""); 

The above code DOESN'T work

P.S There's significant challenges when googling this kind of stuff since the query "R new line character" does seem to confuse google. I really wish R had a different name.

like image 891
MadSeb Avatar asked Feb 16 '12 19:02

MadSeb


People also ask

How do I add a new line in print R?

The most commonly used are "\t" for TAB, "\n" for new-line, and "\\" for a (single) backslash character.

What character is used to print new line in a print if statement?

The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen.

How can we create a newline in a print function?

Explanation. The print() function has an optional keyword argument for the end of the string, called end , which defaults to the OS's newline character, for eg. \n . So, when you're calling print('hello') , Python is actually printing 'hello' + '\n' .

Does newline count as character?

Newline (frequently called line ending, end of line (EOL), next line (NEL) or line break) is a control character or sequence of control characters in a character encoding specification (e.g., ASCII, EBCDIC) that is used to signify the end of a line of text and the start of a new one.


1 Answers

The nature of R means that you're never going to have a newline in a character vector when you simply print it out.

> print("hello\nworld\n") [1] "hello\nworld\n" 

That is, the newlines are in the string, they just don't get printed as new lines. However, you can use other functions if you want to print them, such as cat:

> cat("hello\nworld\n") hello world 
like image 81
David Robinson Avatar answered Nov 09 '22 02:11

David Robinson