[R] Regex and gsub
Bernd Weiss
bernd.weiss at uni-koeln.de
Tue May 11 11:16:55 CEST 2010
Am 11.05.2010 10:37, schrieb arnaud Gaboury:
> Dear group,
>
> Here is my df :
>
> df3 <-
> structure(list(DESCRIPTION = c("COPPER May/10", "COTTON NO.2 Jul/10",
> "CRUDE OIL miNY May/10", "GOLD Jun/10", "ROBUSTA COFFEE (10) Jul/10",
> "SOYBEANS Jul/10", "SUGAR NO.11 Jul/10", "SUGAR NO.11 May/10",
> "WHEAT Jul/10", "SPCL HIGH GRADE ZINC USD", "STANDARD LEAD USD",
> "CORN May/10", "SILVER May/10", "WHEAT May/10", "COFFEE C Jul/10",
> "CORN Jul/10", "HENRY HUB NATURAL GAS May/10"), POSITION = c(0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), prix = c(-14,
> 3.74999999999997, 5.22500000000005, 21.6999999999998, 68, -8.5,
> 2.72999999999999, -0.900000000000002, -64.25, -41, -118, 7.75,
> 45.300, 0, 4.75000000000003, -1.5, -0.0490000000000013)), .Names =
> c("DESCRIPTION",
> "POSITION", "prix"), row.names = c(NA, -17L), class = "data.frame")
>
> I want to remove all the dates in $DESCRIPTION. I was thinking using
> something like this:
>> df3$DESCRIPTION<- gsub("here is a regex expression","",df3$DESCRIPTION).
I am not an expert on regular expressions but the following works for me
df3$DESCRIPTION.new <- gsub("\\s\\w{3}/\\d{2}", "", df3$DESCRIPTION)
HTH,
Bernd
More information about the R-help
mailing list