折叠以“”分隔的元素

时间:2018-08-24 03:16:38

标签: r

我有原始书目数据,如下所示:

bib = 
c("Bernal, Martin, \\\"Liu Shi-p\\'ei and National Essence,\\\" in Charlotte", 
    "Furth, ed., *The Limit of Change, Essays on Conservative Alternatives in", 
    "Republican China*, Cambridge: Harvard University Press, 1976.", 
    "", "Chen,Hsi-yuan, \"*Last Chapter Unfinished*: The Making of the *Draft Qing", 
    "History* and the Crisis of Traditional Chinese Historiography,\"", 
    "*Historiography East & West*2.2 (Sept. 2004): 173-204", "", 
    "Dennerline, Jerry, *Qian Mu and the World of Seven Mansions*, New Haven:", 
    "Yale University Press, 1988.", "")

[1] "Bernal, Martin, \\\"Liu Shi-p\\'ei and National Essence,\\\" in Charlotte"
 [2] "Furth, ed., *The Limit of Change, Essays on Conservative Alternatives in" 
 [3] "Republican China*, Cambridge: Harvard University Press, 1976."            
 [4] ""                                                                         
 [5] "Chen,Hsi-yuan, \"*Last Chapter Unfinished*: The Making of the *Draft Qing"
 [6] "History* and the Crisis of Traditional Chinese Historiography,\""         
 [7] "*Historiography East & West*2.2 (Sept. 2004): 173-204"                    
 [8] ""                                                                         
 [9] "Dennerline, Jerry, *Qian Mu and the World of Seven Mansions*, New Haven:" 
[10] "Yale University Press, 1988."                                             
[11] "" 

我想将""之间的元素折叠成一行,以便:

clean_bib[1]=paste(bib[1], bib[2], bib[3])
clean_bib[2]=paste(bib[5], bib[6], bib[7])
clean_bib[3]=paste(bib[9], bib[10])

[1] "Bernal, Martin, \\\"Liu Shi-p\\'ei and National Essence,\\\" in Charlotte Furth, ed., *The Limit of Change, Essays on Conservative Alternatives in Republican China*, Cambridge: Harvard University Press, 1976."
[2] "Chen,Hsi-yuan, \"*Last Chapter Unfinished*: The Making of the *Draft Qing History* and the Crisis of Traditional Chinese Historiography,\" *Historiography East & West*2.2 (Sept. 2004): 173-204"                
[3] "Dennerline, Jerry, *Qian Mu and the World of Seven Mansions*, New Haven: Yale University Press, 1988."

是否有单线自动执行此操作?

2 个答案:

答案 0 :(得分:2)

您可以在与所有tapply分组时使用"",然后将组粘贴在一起

  unname(tapply(bib,cumsum(bib==""),paste,collapse=" "))
[1] "Bernal, Martin, \\\"Liu Shi-p\\'ei and National Essence,\\\" in Charlotte Furth, ed., *The Limit of Change, Essays on Conservative Alternatives in Republican China*, Cambridge: Harvard University Press, 1976."
[2] " Chen,Hsi-yuan, \"*Last Chapter Unfinished*: The Making of the *Draft Qing History* and the Crisis of Traditional Chinese Historiography,\" *Historiography East & West*2.2 (Sept. 2004): 173-204"               
[3] " Dennerline, Jerry, *Qian Mu and the World of Seven Mansions*, New Haven: Yale University Press, 1988."                                                                                                          
[4] ""                           

您也可以这样做:

  unname(c(by(bib,cumsum(bib==""),paste,collapse=" ")))

 unname(tapply(bib,cumsum(grepl("^$",bib)),paste,collapse=" "))

答案 1 :(得分:1)

类似于其他答案。这使用splitsapply。第二行只是删除仅包含“”的任何元素。

vec <- unname(sapply(split(bib, f = cumsum(bib %in% "")), paste0, collapse = " "))
vec[!vec %in% ""]
相关问题