2016-11-29 6 views
1

R:コンマ

string <- "field1,field2,\"there is a , in field3\", field4, \"2,456\", field6" 

負の試みに応じて、カンマを含む文字列を分割する方法:カンマを含むフィールドが\" ... \"の間であることを

test <- unlist(strsplit(noquote(string), ",")) 
test <- gsub("[^A-Za-z0-9' ']", "", test) 
gsub("^\\s+|\\s+$", "", test) 

[1] "field1"  "field2"  "there is a" "in field3" 
[5] "field4"  "2"   "456"  "field6"  

注意を。

+0

あなたの理想的な結果は何ですか? – Nate

+2

read.table(text = string、sep = "、")を試してください。引用符で囲まれたカンマは無視されます。それはあなたの例に作用します。 – gfgm

+0

私は単純な答えが大好きです:-) 答えに変換すると、私はそれを受け入れます! ありがとう! – Marco

答えて

2

read.table関数は、引用符で囲まれたカンマを無視します。

string <- "field1,field2,\"there is a , in field3\", field4, \"2,456\", field6" 

desired_result <- read.table(text=string, sep=",")