R - do I need to add explicit new line character w

2019-01-08 15:05发布

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.

2条回答
Anthone
2楼-- · 2019-01-08 15:29

Example on NewLine Char:

for (i in 1:5)
  {
   for (j in 1:i)
    {
     cat(j)
    }
    cat("\n")
  }

Result:

    1
    12
    123
    1234
    12345
查看更多
疯言疯语
3楼-- · 2019-01-08 15:40

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
查看更多
登录 后发表回答