Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)
edited by

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 are 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.

1 Answer

0 votes
by
edited by

When you simply print out a vector, the nature of R is such that you're never going to have a newline in a character vector.

> 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

You can also use the writelines() function as follows:

writeLines("hello\nworld")

hello

world

If you want to explore more in R programming then watch this R programming tutorial for beginner:

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...