【发布时间】:2019-05-14 05:20:00
【问题描述】:
我有从 .csv 文件导入的数据。第一列包含包含括号内的文本的字符串。数据如下:
symbol
___________________________________________
1 | Apollo Senior Floating Rate Fund Inc. (AFT)
2 | Apollo Tactical Income Fund Inc. (AIF)
3 | Altra Industrial Motion Corp. (AIMC)
4 | Allegion plc (ALLE)
5 | Amphenol Corporation (APH)
6 | Ares Management Corporation (ARES)
7 | ARMOUR Residential REIT, Inc. (ARR)
8 | Banc of California, Inc. (BANC)
9 | BlackRock Resources (BCX)
10| Belden Inc (BDC)
...
我需要将该列数据转换成一个列表,例如:
symbol2
___________________________________________
1 | AFT
2 | AIF
3 | AIMC
4 | ALLE
5 | APH
6 | ARES
7 | ARR
8 | BANC
9 | BCX
10| BDC
...
我的最终目标是获得一个字符串,其中由括号绑定的文本由“;”分隔像这样:
"AFT;AIF;AIMC;ALLE;APH;ARES;ARR;BANC;BCX;BDC;..."
我可以用
完成这最后一步paste(symbol2, collapes = ";")
但我不知道如何隔离所需的文本。
我已经尝试了此处列出的所有内容 (extract a substring in R according to a pattern),方法是将“:”替换为“(”,但无法正常工作。我尝试过:
gsub("(?<=\\()[^()]*(?=\\))(*SKIP)(*F)|.", "", symbol, perl=T)
这里推荐(Extract text in parentheses in R),但输出是
"c(4, 5, 2, 1, 3, 6, 7, 8, 17, 9,...)"
有什么帮助吗?
【问题讨论】:
标签: r regex string character parentheses