如何使用正则表达式正确编辑字符串& R'

时间:2012-05-07 08:44:46

标签: regex r list vector dataframe

我将制表符分隔的文本文件中的数据导入到data.frame中。现在我想调整和编辑名为C1的列的内容。为此我建议使用正则表达式。起初我用过:

for (rn in 1:length(C1))
C1s <- strsplit(as.character(C1[rn]), "; ", fixed = TRUE)[[1]]

将以下示例中的各个条目分开:

  • [Zhang,Junling] China Agr Univ,Coll Resources&amp;环境科学,北京100094,中华人民共和国; [张俊玲] Univ Hohenheim,Inst Plant Nutr,D-7000斯图加特,德国; [George,Eckhard] Humboldt Univ,Inst Crop Sci,Dept Plant Nutr,D-14979 Grossbeeren,Germany; [George,Eckhard] Leibniz Inst Vegetable&amp;观赏作物Theodor,D-14979 Grossbeeren,德国
  • UNIV DORTMUND,INST PHYS,D-44221 DORTMUND,GERMANY; HUMBOLDT UNIV,INST PHYS,D-15738 ZEUTHEN,GERMANY
  • UNIV KEELE,DEPT CHEM,KEELE ST5 5BG,STAFFS,ENGLAND; MT SINAI HOSP,MT SINAI SCH MED,DR A A FISHBERG CTR NEUROBIOL,NY YORK,NY 10029; HUMBOLDT UNIV BERLIN,CHARITE HOSP,DEPT PATHOL&amp; CLIN BIOCHEM,柏林,德国
  • NIFAD,MOLEC MICROBIOL LAB,BETHESDA,MD

然后我想

  • 将所有内容设置为大写
  • 用“HUMBOLDT”替换字符串“Humboldt ... Germany”用“UNITED KINGDOM”替换字符串“England”,“Scotland”,“Wales”和“North Ireland”
  • 将美国州缩写和邮政编码的组合替换为“USA”
  • 将美国州缩写替换为“USA”

此外,我想删除除分号(包括它们后面的空格),直接在它们前面的单词以及上面示例中的最后一个单词之外的所有内容。
我用了

gsub('[.*\\] ', ''(toupper(C1s))  

gsub(',\\s*', ','(toupper(C1s))  

,例如,但无法使其正常工作 我想获得以下输出:

  • PEOPLES R CHINA;德国; HUMBOLDT;德国
  • 德国; HUMBOLDT
  • 英国;美国; HUMBOLDT
  • 美国

所以我的问题是:我怎样才能达到我想要的结果呢? 非常感谢您提前考虑!

更新和其他问题

感谢mrdwab的有用回复和评论,我取得了很大进展。

不幸的是,直到现在我才意识到还有一些地址,如方括号内有多位作者。遗憾的是,mrdwab提出的算法无法正常使用这些算法。

> test = c("[Bocquet, F. C.; Giovanelli, L.; Abel, M.; Porte, L.; Themlin, J. -M.] Aix Marseille Univ, Inst Mat Microelect & Nanosci Prov IM2NP, F-13397 Marseille 20, France; [Bocquet, F. C.; Giovanelli, L.; Abel, M.; Porte, L.; Themlin, J. -M.] CNRS, Inst Mat Microelect & Nanosci Prov IM2NP, UMR 6242, Marseille, France; [Amsalem, P.; Koch, N.] Humboldt Univ, Inst Phys, D-12489 Berlin, Germany; [Petaccia, L.; Topwal, D.; Gorovikov, S.; Goldoni, A.] Sincrotrone Trieste, I-34149 Trieste, Italy")

这是我得到的结果:

> test
[1] "[BOCQUET,  F. C." "GIOVANELLI,  L."  "ABEL,  M."        "PORTE,  L."      
[5] "FRANCE"           "[BOCQUET,  F. C." "GIOVANELLI,  L."  "ABEL,  M."       
[9] "PORTE,  L."       "FRANCE"           "[AMSALEM,  P."    "HUMBOLDT"        
[13] "[PETACCIA,  L."   "TOPWAL,  D."      "GOROVIKOV,  S."   "ITALY" 

这是我想要的结果:

[1] "FRANCE"; "FRANCE"; "HUMBOLDT"; "ITALY"

我尝试使用它来分别删除每个方括号及其内容:

C1s = gsub("(.*)[(.*)]", "\\2", C1s)

但是,第一个开始括号和最后一个右括号之间的所有内容都被删除了... 如果我先用逗号替换方括号内的所有分号,它可能会工作吗?我试过了

C1s = gsub("[(.*);(.*)]", "[(.*),(.*)]", C1s)

要做到这一点,但它没有用。
所以我很感谢你在这方面的帮助!

除此之外,我仍然坚持另一个障碍,我似乎无法独自解决,不幸的是...... 这是我目前的输出:

> C1s
[1] "PEOPLES R CHINA" "GERMANY"         "HUMBOLDT"         "GERMANY"
[5] "GERMANY"         "HUMBOLDT"        "UNITED KINGDOM"   "USA"
[9] "HUMBOLDT"        "USA"
> dims
[1] 4 2 3 1
> is.list(C1)
[1] FALSE
> is.vector(C1)
[1] TRUE

但我究竟如何使用 dims 中的信息来创建所需的输出?:

[1] "PEOPLES R CHINA"; "GERMANY"; "HUMBOLDT"; "GERMANY"
[2] "GERMANY"; "HUMBOLDT"
[3] "UNITED KINGDOM"; "USA"; "HUMBOLDT"
[4] "USA"

非常感谢您的支持!

1 个答案:

答案 0 :(得分:3)

假设您的数据如下所示:

test = c("[Zhang, Junling] China Agr Univ, Coll Resources & Environm Sci, Beijing 100094, Peoples R China", 
         " [Zhang, Junling] Univ Hohenheim, Inst Plant Nutr, D-7000 Stuttgart, Germany", 
         " [George, Eckhard] Humboldt Univ, Inst Crop Sci, Dept Plant Nutr, D-14979 Grossbeeren, Germany", 
         " [George, Eckhard] Leibniz Inst Vegetable & Ornamental Crops Theodor, D-14979 Grossbeeren, Germany"
)

首先,让我们全部大写:

test = toupper(test)

然后,让我们将HUMBODT ... GERMANY更改为HUMBOLDT

test = gsub("(.*)(HUMBOLDT)(.*)", "\\2", test)

然后,让我们只提取字符串的最后一部分。

test = gsub("(.*), +([A-Z ]+$)", "\\2", test)
> test
[1] "PEOPLES R CHINA" "GERMANY"         "HUMBOLDT"        "GERMANY"    

如果我有一个更完整的例子,我也可以帮助你处理其他部分。

更新

这是一个最常用的解决方案,可以帮助你弄清楚该怎么做。

使用“扫描”从帖子中复制数据,我们可以获得如下所示的数据:

test = c("[Zhang, Junling] China Agr Univ, Coll Resources & Environm Sci, Beijing 100094, Peoples R China; [Zhang, Junling] Univ Hohenheim, Inst Plant Nutr, D-7000 Stuttgart, Germany; [George, Eckhard] Humboldt Univ, Inst Crop Sci, Dept Plant Nutr, D-14979 Grossbeeren, Germany; [George, Eckhard] Leibniz Inst Vegetable & Ornamental Crops Theodor, D-14979 Grossbeeren, Germany", 
         "UNIV DORTMUND,INST PHYS,D-44221 DORTMUND,GERMANY; HUMBOLDT UNIV,INST PHYS,D-15738 ZEUTHEN,GERMANY", 
         "UNIV KEELE,DEPT CHEM,KEELE ST5 5BG,STAFFS,ENGLAND; MT SINAI HOSP,MT SINAI SCH MED,DR A A FISHBERG CTR NEUROBIOL,NEW YORK,NY 10029; HUMBOLDT UNIV BERLIN,CHARITE HOSP,DEPT PATHOL & CLIN BIOCHEM,BERLIN,GERMANY", 
         "NIFAD,MOLEC MICROBIOL LAB,BETHESDA,MD")

您需要一步一步地进行所需的转换。我就是这样做的。

test.orig = test # A backup, just in case
test = toupper(test) # To uppercase
test = strsplit(test, ";") # Split everything up into a list
dims = sapply(test, length) # We might need this later
test = unlist(test) # Now, a single vector
test = gsub(",", " , ", test) # Pad those commas with some space
# Replace any 'HUMBOLDT'...'GERMANY' stuff with 'HUMBOLDT'
test = gsub("(.*)(HUMBOLDT)(.*)(GERMANY$)", "\\2", test)
# Replace a bunch of stuff with 'UNITED KINGDOM'
test = gsub("ENGLAND|SCOTLAND|WALES|NORTH IRELAND", "UNITED KINGDOM", test)
# Search for a pair of letters followed by a space followed by five digits
# and replace it with USA
test = gsub(" [A-Z]{2} [0-9]{5}", " USA", test)
# Find the item following the last comma
test = gsub("(.*), ([A-Z ]+)$", "\\2", test)
# Remove any whitespace
test = gsub("^ | $", "", test)
> test
[1] "PEOPLES R CHINA" "GERMANY"         "HUMBOLDT"        "GERMANY"        
[5] "GERMANY"         "HUMBOLDT"        "UNITED KINGDOM"  "USA"            
[9] "HUMBOLDT"        "MD"                  
# Unlist it if you need to
> split(test, rep(dims, dims))
$`1`
[1] "MD"

$`2`
[1] "GERMANY"  "HUMBOLDT"

$`3`
[1] "UNITED KINGDOM" "USA"            "HUMBOLDT"      

$`4`
[1] "PEOPLES R CHINA" "GERMANY"         "HUMBOLDT"        "GERMANY"   

但是,我将告诉你如何用美国替换状态缩写,以及如何将它放回到相同的行顺序。