相关疑难解决方法(0)

如何将字符列表折叠为R中的单个字符串

有一个列表我想作为单个字符串输出到excel文件中.我从一个字符列表开始.

  url="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=21558518&retmode=xml"
  xml = xmlTreeParse(url,useInternal = T)
  ns <- getNodeSet(xml, '//PublicationTypeList/PublicationType')  
  types <- sapply(ns, function(x) { xmlValue(x) } )  
  types
Run Code Online (Sandbox Code Playgroud)

输出是这样的:

[1] "Journal Article"                      "Multicenter Study"                    "Research Support, N.I.H., Extramural"
[4] "Research Support, Non-U.S. Gov't"    
Run Code Online (Sandbox Code Playgroud)

所以在类型中 - 有一个字符列表现在我需要制作一个字符串.这是我到目前为止,但它不是最佳的:

 types_as_string = as.character(types[[1]])
      if (length(types) > 1) for (j in 2:length(types))   types_as_string = paste(types_as_string,"| ",as.character(types[[j]]),sep="")
 types_as_string          
 [1] "Journal Article| Multicenter Study| Research Support, N.I.H., Extramural| Research Support, Non-U.S. Gov't"
Run Code Online (Sandbox Code Playgroud)

所以我想最终得到一个由管道或其他分隔符分隔的漂亮字符串.(最后一个代码部分 - 我想要很好地重写).管道很重要,必须妥善完成.

string r string-concatenation

38
推荐指数
4
解决办法
5万
查看次数

标签 统计

r ×1

string ×1

string-concatenation ×1