【发布时间】:2015-08-19 09:24:59
【问题描述】:
我有一个格式如下的字符串:
a <- c("first_name=James(Mr), cust_id=98503(ZZW_LG,WGE,zonaire),
StartDate=2015-05-20, EndDate=2015-05-20, performance=best")
我的目标是在如下数据框中获得最终结果:
first_name cust_id start_date end_date performance cust_notes
James(Mr) 98503 2015-05-20 2015-05-20 best ZZW_LG,WGE,zonaire
我运行了以下代码:
a <- c("first_name=James(Mr), cust_id=98503(ZZW_LG,WGE,zonaire),
StartDate=2015-05-20, EndDate=2015-05-20, performance=best")
split_by_comma <- strsplit(a,",")
split_by_equal <- lapply(split_by_comma,strsplit,"=")
由于 custid 有额外的逗号和括号,我没有得到想要的结果。
请注意,名字中的括号是真实的,需要原样。
【问题讨论】:
-
根据您的示例,使用
split_by_comma <- strsplit(a,", ")(逗号后有一个空格)应该可以解决问题。编辑:在尝试您的代码时,我被回车击中,不得不使用两个拆分选项切换到split_by_comma <- strsplit(a,c(", ","\n"),逗号和空格或换行符。仅当您的数据中没有逗号后跟空格时,这才有效。 (不是真正的答案,所以作为评论发布)