首页 > 解决方案 > 在拆分元素之前合并

问题描述

我有一个字符向量。我想组合 value 不是 separator 的相邻行\n。在下面的向量中,这是两个元素。

 "Amount of pensions received mens.",   "(Grant data) (Pension Received (Monthly Basis))"

它应该结合为"Amount of pensions received mens. (Grant data) (Pension Received (Monthly Basis))"

mat <- c("Dolphin Sentimental S.r.l.", "\n", "Tiger Sentiyapa S.r.l.", 
      "\n", "Effort rate calculates to grant (Debt to Income Rate)", 
      "\n", "Amount of pensions received mens.", "(Grant data) (Pension Received (Monthly Basis))", 
      "\n", "Effort rate calculates to grant (Debt to Income Rate)", 
      "\n", "Amount of pensions received mens.", "(Grant data) (Pension Received (Monthly Basis))"
    )

期望的输出

[1] "Dolphin Sentimental S.r.l."                                                       
[2] "Tiger Sentiyapa S.r.l."                                                           
[3] "Effort rate calculates to grant (Debt to Income Rate)"                            
[4] "Amount of pensions received mens. (Grant data) (Pension Received (Monthly Basis))"
[5] "Effort rate calculates to grant (Debt to Income Rate)"                            
[6] "Amount of pensions received mens. (Grant data) (Pension Received (Monthly Basis))"

标签: r

解决方案


#paste together (collapse to 1 long string), then split using '\n' as separator
strsplit(paste0(mat, collapse = ""), "\\n")

# [[1]]
# [1] "Dolphin Sentimental S.r.l."                                                      
# [2] "Tiger Sentiyapa S.r.l."                                                          
# [3] "Effort rate calculates to grant (Debt to Income Rate)"                           
# [4] "Amount of pensions received mens.(Grant data) (Pension Received (Monthly Basis))"
# [5] "Effort rate calculates to grant (Debt to Income Rate)"                           
# [6] "Amount of pensions received mens.(Grant data) (Pension Received (Monthly Basis))"

推荐阅读