【发布时间】:2016-04-27 22:18:04
【问题描述】:
我有一个字符向量,其中单个元素包含多个用逗号分隔的字符串。我通过从数据框中提取它获得了这个列表,它看起来像这样:
[1] "Acworth, Crescent Lake, East Acworth, Lynn, South Acworth"
[2] "Ferncroft, Passaconaway, Paugus Mill"
[3] "Alexandria, South Alexandria"
[4] "Allenstown, Blodgett, Kenison Corner, Suncook (part)"
[5] "Alstead, Alstead Center, East Alstead, Forristalls Corner, Mill Hollow"
[6] "Alton, Alton Bay, Brookhurst, East Alton, Loon Cove, Mount Major, South Alton, Spring Haven, Stockbridge Corners, West Alton, Woodlands"
[7] "Amherst, Baboosic Lake, Cricket Corner, Ponemah"
[8] "Andover, Cilleyville, East Andover, Halcyon Station, Potter Place, West Andover"
[9] "Antrim, Antrim Center, Clinton Village, Loverens Mill, North Branch"
[10] "Ashland"
我想获得一个新的字符向量,其中每个字符串都是该字符向量中的一个元素,即:
[1] "Acworth", "Crescent Lake", "East Acworth", "Lynn", "South Acworth"
[6] "Ferncroft", "Passaconaway", "Paugus Mill", "Alexandria", "South Alexandria"
我使用了strsplit() 函数,但这会返回一个列表。当我尝试将其转换为字符向量时,它会恢复到旧状态。
我确信这是一个非常简单的问题 - 任何帮助将不胜感激!谢谢!
【问题讨论】:
-
在运行
strsplit之后运行unlist而不是运行as.character -
多么简单!太棒了,非常感谢!
-
见the demo。顺便说一句,有空格 - 你想摆脱它们吗?
-
@WiktorStribiżew
", "而不是"," -
@DavidArenburg:我在上面的链接中更新了演示。