r - 在拆分元素之前合并
问题描述
我有一个字符向量。我想组合 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))"
解决方案
#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))"
推荐阅读
- python - Scipy rv_continuous 错误地从分布中生成样本
- php - 有没有办法限制供应商的包版本要求
- python - 为什么 (3.3==np.asarray([3.3])) 等于 [True] 而不是 False?
- amazon-web-services - 如何在 AWS CodeBuild cloudformation 模板中设置分支过滤器选项?
- mysql - mysql / sql:如何删除除每个用户的最后 N 个行之外的所有行?
- python - 遗漏一个不明确的元素
- java - 如何验证 spring-boot Web 服务器是否正在运行
- android - 未经 WRITE_SETTINGS 明确许可设置自定义铃声
- python - 比较两个字典时输入错误
- java - 在 protobuf 中重命名枚举值 - 向后兼容性