How to collapse a list of characters into a single string in R

后端 未结 3 924
天涯浪人
天涯浪人 2020-12-06 08:59

There is a list which I would like to output into an excel file as a single string. I start with a list of characters.

  url=\"http://eutils.ncbi.nlm.nih.gov         


        
相关标签:
3条回答
  • 2020-12-06 09:27

    You can do it with paste function

        > paste(c('A', 'B', 'C'), collapse=', ' )
        [1] "A, B, C"
    
    0 讨论(0)
  • 2020-12-06 09:36

    The shortest method is to use the base toString function. In any case the output string will contain commas

    vector<-c('A', 'B', 'C')
    
    toString(vector)
    

    if you don't want commas

    gsub(",","",toString(vector))
    

    If you want any other separator you can substitute anything you like in gsub instead of replacing commas with nothing

    0 讨论(0)
  • 2020-12-06 09:41

    You can do it with str_c function

    > library('stringr')
    > str_c(c('A','B','C'),collapse=',')    
    [1] "A,B,C"
    
    0 讨论(0)
提交回复
热议问题