如何区分CSV文件中特定列的变量并写入R中的CSV文件? [英] How to separate the variables of a particular column in a CSV file and write to a CSV file in R?

查看:203
本文介绍了如何区分CSV文件中特定列的变量并写入R中的CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV档案,例如

  Market,CampaignName,Identity 
Wells Fargo,Gary In MetroChicago IL Metro ,56
EMC,洛杉矶CA MetroBoston MA Metro,78
Apple,Cupertino CA Metro,68

希望输出到以第一行为标题的CSV文件

 市场,城市, 
Wells Fargo,Gary,IN,56
Wells Fargo,Chicago,IL,56
EMC,Los Angeles,CA,78
EMC,Boston,MA,78
Apple,Cupertino,CA,68

res< -
gsub('*。)[AZ] {2} 2})。*','\\1,\\2:\\3,\\4',
xx $ Market)

如何修改上述正则表达式以获得R中的结果?

解决方案

 
xx.to.split< - with(xx,setNames(gsub(Metro,,as.character(CampaignName)),Market))
do.call(rbind,str_match_all (xx.to.split,(。+?)([AZ] {2})?))[,-1]

产生:

  [,1] [,2] 
Wells Fargo GaryIN
Wells FargoChicagoIL
EMCLos AngelesCA
EMCBostonMA
AppleCupertino

即使您在每个市场中拥有不同数量的不幸的是,我认为基本选项是恼人的实现,因为令人沮丧的是,没有一个 gregexec ,虽然我会很好奇,如果有人提出一些相当紧凑的基地。 p>

I have a CSV file like

Market,CampaignName,Identity
Wells Fargo,Gary IN MetroChicago IL Metro,56
EMC,Los Angeles CA MetroBoston MA Metro,78
Apple,Cupertino CA Metro,68

Desired Output to a CSV file with the first row as the headers

Market,City,State,Identity
Wells Fargo,Gary,IN,56
Wells Fargo,Chicago,IL,56
EMC,Los Angeles,CA,78
EMC,Boston,MA,78
Apple,Cupertino,CA,68

res <- 
  gsub('(.*) ([A-Z]{2})*Metro (.*) ([A-Z]{2}) .*','\\1,\\2:\\3,\\4',
  xx$Market)

How to modify the above regular expressions to get the result in R? New to R, any help is appreciated.

解决方案

library(stringr)
xx.to.split <- with(xx, setNames(gsub("Metro", "", as.character(CampaignName)), Market))
do.call(rbind, str_match_all(xx.to.split, "(.+?) ([A-Z]{2}) ?"))[, -1]

Produces:

            [,1]          [,2]
Wells Fargo "Gary"        "IN"
Wells Fargo "Chicago"     "IL"
EMC         "Los Angeles" "CA"
EMC         "Boston"      "MA"
Apple       "Cupertino"   "CA"

This should work even if you have different number of Compaign Names in each market. Unfortunately I think base options are annoying to implement because frustratingly there isn't a gregexec, although I'd be curious if someone comes up with something comparably compact in base.

这篇关于如何区分CSV文件中特定列的变量并写入R中的CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆