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

后端 未结 3 1826
鱼传尺愫
鱼传尺愫 2020-11-30 02:34

How do I use the new line character in R?

myStringVariable <- \"Very Nice ! I like\";

myStringVariabel <- paste(myStringVariable, \"\\n\", sep=\"\");         


        
相关标签:
3条回答
  • 2020-11-30 02:39

    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
    
    0 讨论(0)
  • 2020-11-30 02:53

    Example on NewLine Char:

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

    Result:

        1
        12
        123
        1234
        12345
    
    0 讨论(0)
  • 2020-11-30 02:53

    You can also use writeLines.

    > writeLines("hello\nworld")
    hello
    world
    

    And also:

    > writeLines(c("hello","world"))
    hello
    world
    
    0 讨论(0)
提交回复
热议问题