【发布时间】:2017-01-06 19:22:19
【问题描述】:
我想在一个复杂的字符串中保留一个字符串。我认为我可以使用正则表达式来保留我需要的东西。基本上,我只想在Function=\"SMAD5\" 中保留\" 和\" 之间的信息。我也想保留空字符串:Function=\"\"
df=structure(1:6, .Label = c("ID=Gfo_R000001;Source=ENST00000513418;Function=\"SMAD5\";",
"ID=Gfo_R000002;Source=ENSTGUT00000017468;Function=\"CENPA\";",
"ID=Gfo_R000003;Source=ENSGALT00000028134;Function=\"C1QL4\";",
"ID=Gfo_R000004;Source=ENSTGUT00000015300;Function=\"\";", "ID=Gfo_R000005;Source=ENSTGUT00000019268;Function=\"\";",
"ID=Gfo_R000006;Source=ENSTGUT00000019035;Function=\"\";"), class = "factor")
这应该是这样的:
"SMAD5"
"CENPA"
"C1QL4"
NA
NA
NA
到目前为止,我能做的:
gsub('.*Function=\"',"",df)
[1] "SMAD5\";" "CENPA\";" "C1QL4\";" "\";" "\";" "\";"
但我被一堆\";" 困住了。如何用一行删除它们?
我试过这个:
gsub('.*Function=\"' & '.\"*',"",test)
但它给了我这个错误:
Error in ".*Function=\"" & ".\"*" :
operations are possible only for numeric, logical or complex types
【问题讨论】:
-
试试
gsub('.*Function=\"([^\"]*).*',"\\1",df) -
谢谢!成功了!