Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用“分隔”分隔的折叠元素&引用;_R - Fatal编程技术网

R 用“分隔”分隔的折叠元素&引用;

R 用“分隔”分隔的折叠元素&引用;,r,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.",

我有如下原始书目数据:

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."

是否有一个班轮可以自动执行此操作

在使用所有
分组时,您可以使用
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=" ")))


etc

您可以在使用所有
进行分组时使用
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=" ")))


etc与另一个答案类似。这使用了
split
sapply
。第二行只是删除任何只有has“”的元素


vec与另一个答案相似。这使用了
split
sapply
。第二行只是删除任何只有has“”的元素


vec-Ahh。。。我想我明白了。试着运行
cumsum(bib==”)
,看看它能提供什么。这就是分组。即前三个0表示前三个字符串应分组在一起,后四个0表示后四个字符串应分组在一起,三个2表示后三个字符串应分组在一起。现在我们用
tapply
来做这个。。。我想我明白了。试着运行
cumsum(bib==”)
,看看它能提供什么。这就是分组。即前三个0表示前三个字符串应分组在一起,后四个0表示后四个字符串应分组在一起,三个2表示后三个字符串应分组在一起。现在我们使用
tapply
来执行此操作