Difference between paste() and paste0()

前端 未结 3 1751
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 08:16

Being new to R, can someone please explain the difference between paste() and paste0(), what I had understood from some post is that

p         


        
3条回答
  •  孤城傲影
    2021-01-31 08:28

    In simple words,

    paste() is like concatenation using separation factor, whereas,

    paste0() is like append function using separation factor.

    Adding some more references to above discussion, below try outs can be useful to avoid confusion:

    > paste("a","b")  #Here default separation factor is " " i.e. a space
    
    [1] "a b"  
    
    > paste0("a","b") #Here default separation factor is "" i.e a null
    
    [1] "ab"
    
    > paste("a","b",sep="-")
    
    [1] "a-b"
    
    > paste0("a","b",sep="-")
    
    [1] "ab-"
    
    > paste(1:4,"a")
    
    [1] "1 a" "2 a" "3 a" "4 a"
    
    > paste0(1:4,"a")
    
    [1] "1a" "2a" "3a" "4a"
    
    > paste(1:4,"a",sep="-")
    
    [1] "1-a" "2-a" "3-a" "4-a"
    
    > paste0(1:4,"a",sep="-")
    
    [1] "1a-" "2a-" "3a-" "4a-"
    

提交回复
热议问题