R: separate a series of string by comma

时间:2018-04-20 21:26:26

标签: r

I have a series of string marked by quotation marks and I'd like them to be separated by comma so that I can pass them to a vector. I tried paste command but I kept getting error messages. Thank you. The data is like:

"Alabama"     "Arkansas"    "Connecticut" "Georgia"     "Hawaii"     "Minnesota"   "Mississippi" "Missouri"    "Oregon" 

1 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

str <- '"Alabama"     "Arkansas"    "Connecticut" "Georgia"     "Hawaii"     "Minnesota"   
"Mississippi" "Missouri"    "Oregon"'


gsub('\\"',"",gsub('\\"\\s+\\"',",",str))

#[1] "Alabama,Arkansas,Connecticut,Georgia,Hawaii,Minnesota,Mississippi,Missouri,Oregon"

<强>正则表达式:

  • \\"\\s+\\" - 在"中查找多个空格并替换为","