如何在CSV中分隔CSV行并从中生成另一个CSV文件?

时间:2014-02-25 22:48:44

标签: r csv

我有一个像

这样的CSV文件
AdvertiserName,CampaignName
Wells Fargo,Gary IN MetroChicago IL Metro
EMC,Los Angeles CA MetroBoston MA Metro
Apple,Cupertino CA Metro

R中所需的输出

AdvertiserName,City,State
Wells Fargo,Gary,IN
Wells Fargo,Chicago,IL
EMC,Los Angeles,CA
EMC,Boston,MA
Apple,Cupertino,CA

我做过像

record <- read.csv("C:/Users/Administrator/Downloads/Campaignname.csv",header=TRUE)
ad <- record$AdvertiserName
camp <- record$CampaignName
read.table(text=gsub('Metro', '\n', c), col.names=c('City', 'State'))

它会抛出错误。

如何获得理想的结果? 提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以这样做:

## read the csv file, you change text here by your fileName
xx <- read.table(text ='AdvertiserName,CampaignName
Wells Fargo,Gary INMetro Chicago IL Metro
EMC,Los Angeles CAMetro Boston MA Metro',sep=',',header=TRUE)

##  use regular expression to create city and state variables
##  rows are separated  by ":"
##  columns are separated by a comma ","
res <- 
gsub('(.*) ([A-Z]{2})*Metro (.*) ([A-Z]{2}) .*','\\1,\\2:\\3,\\4',
     xx$CampaignName)
## Use strsrsplit to extract rows and columns
## This is a compacted code !
yy <- 
Map(function(x,y)
         cbind.data.frame(y,do.call(rbind,strsplit(x,','))),
       strsplit(res,':'),xx$AdvertiserName)
## create the final data.frame and set names 
res <- do.call(rbind,yy)
setNames(res, c('AdvertiserName','City','State'))

  AdvertiserName        City State
1    Wells Fargo        Gary    IN
2    Wells Fargo     Chicago    IL
3            EMC Los Angeles    CA
4            EMC      Boston    MA