【问题标题】:How to modify regex expression in R?如何修改R中的正则表达式?
【发布时间】:2014-02-26 06:16:02
【问题描述】:

我有一个 CSV 文件

AdvertiserName,Market
Wells Fargo,Gary INMetro Chicago IL Metro
EMC,Los Angeles CAMetro Boston MA Metro
Apple,Cupertino CA Metro

而正则表达式中的表达式是

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

现在“市场”列就像“Gary IN MetroChicago IL Metro”而不是“Gary INMetro Chicago IL Metro”,CSV 文件就像

AdvertiserName,CampaignName
Wells Fargo,Gary IN MetroChicago IL Metro
EMC,Los Angeles CA MetroBoston MA Metro
Apple,Cupertino CA Metro

如何修改正则表达式中的表达式以获得所需的输出

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

R 新手。感谢任何帮助。

【问题讨论】:

  • “现在“市场”列就像“Gary IN MetroChicago IL Metro”而不是“Gary IN MetroChicago IL Metro””。嗯?有什么区别?
  • @Hugh :已更改,感谢您的注意。

标签: regex r csv


【解决方案1】:

这是strsplit的一种方式:

# read file
dat <- read.csv("filename.csv", stringsAsFactors = FALSE)

# split strings
splitted <- strsplit(dat$CampaignName, 
                     "( (?=[A-Z]{2}))|((?<=[A-Z]{2}) [A-Z][a-z]+)", perl = TRUE)
# [[1]]
# [1] "Gary"    "IN"      "Chicago" "IL"     
#
# [[2]]
# [1] "Los Angeles" "CA"          "Boston"      "MA"         
#
# [[3]]
# [1] "Cupertino" "CA"       

# create one data frame
setNames(as.data.frame(do.call(rbind, 
                               mapply(cbind, 
                                      dat$AdvertiserName, 
                                      lapply(splitted, function(x)
                                        matrix(x, ncol = 2, byrow = TRUE))))), 
         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
# 5          Apple   Cupertino    CA

【讨论】:

  • 在哪里学习 R 的正则表达式?任何想法,也感谢您的建议和答案。
  • @user3188390 你看过?regex吗?
【解决方案2】:

这有点脏。欢迎编辑。

 # Read in the csv file (saved here as a .txt) to 
    y <- readLines("Stackoverflow20140226.txt")


# Every time see a state, shove a comma in
for (i in seq(y)){
y[[i]] <- gsub("([A-Z]{2}) ", "\\1, ", y[[i]])
}

tf <- tempfile()
writeLines(y, tf)

# Trick the csv file into thinking there are more columns
ncol <- max(count.fields(tf, sep = ","))
x <- read.csv(tf, fill = TRUE, header = FALSE, skip=1,
         col.names = paste("V", seq_len(ncol), sep = ""))
unlink(tf)
# Use reshape to melt the data frame
library(reshape2)
xx <- melt(x, id.vars=1, measure.vars = 2:ncol(x))

xx$variable <- NULL
names(xx) <- c("AdvertiserName", "CampaignName")

xx
  AdvertiserName     CampaignName
1    Wells Fargo          Gary IN
2            EMC   Los Angeles CA
3          Apple     Cupertino CA
4    Wells Fargo  MetroChicago IL
5            EMC   MetroBoston MA
6          Apple            Metro
7    Wells Fargo            Metro
8            EMC            Metro
9          Apple        

【讨论】:

  • stackoverflow.com/questions/22032481/… ,请参考 URL,这会给你一个更好的想法,感谢您的帮助,感谢您的时间和精力,也许解决方案与我的要求略有不同,因为使用 for 循环在 R 中,我的经验不是一个好主意,而且我有数百万行数据。任何进一步的帮助表示赞赏。
猜你喜欢
  • 2022-11-19
  • 2014-04-14
  • 1970-01-01
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多