[R] regex and sub

Bernd Weiss bernd.weiss at uni-koeln.de
Fri May 14 12:23:12 CEST 2010


Am 14.05.2010 12:10, schrieb arnaud Gaboury:
> Dear group,
> 
> Here is an object:
> 
> z <-
> c("LSCPos100415.csv", "LSCPos100416.csv", "LSCPos100419.csv", 
> "LSCPos100420.csv", "LSCPos100421.csv", "LSCPos100422.csv",
> "LSCPos100423.csv", 
> "LSCPos100426.csv", "LSCPos100427.csv", "LSCPos100428.csv",
> "LSCPos100429.csv", 
> "LSCPos100430.csv", "LSCPos100503.csv", "LSCPos100504.csv",
> "LSCPos100505.csv", 
> "LSCPos100506.csv", "LSCPos100507.csv", "LSCPos100510.csv",
> "LSCPos100511.csv", 
> "LSCPos100512.csv", "LSCPos100513.csv")
> 
> I want to keep only the numeric part of each element.
> I thought using something like :
> 
>> sub("REGEX","",y)
> 
> I tried something with " \\D+", but it is not the final result I want
> 
>> sub("\\D+","",y)
>  [1] "100415.csv" "100416.csv" "100419.csv" "100420.csv" "100421.csv"
> "100422.csv" "100423.csv" "100426.csv" "100427.csv" "100428.csv"
> "100429.csv" "100430.csv" "100503.csv" "100504.csv" "100505.csv"
> [16] "100506.csv" "100507.csv" "100510.csv" "100511.csv" "100512.csv"
> "100513.csv"


> gsub("\\D","",z)
 [1] "100415" "100416" "100419" "100420" "100421" "100422" "100423" "100426"
 [9] "100427" "100428" "100429" "100430" "100503" "100504" "100505" "100506"
[17] "100507" "100510" "100511" "100512" "100513"

HTH,

Bernd



More information about the R-help mailing list