[R] gsub,regex and replacing

arnaud Gaboury arnaud.gaboury at gmail.com
Wed Apr 28 13:13:18 CEST 2010


Dear group,

I need to modify some characters in a data frame. I want to use gsub and the
regex functionalities to do this.

Here is the data frame (df):

                      DESCRIPTION    prix quantity
1                     CORN Jul/10    -1.5        0
2                     CORN May/10 -1082.0       -3
3      ROBUSTA COFFEE (10) Jul/10 11084.0        8
4                 SOYBEANS Jul/10  1983.5        2
5 SPCL HIGH GRADE ZINC USD Jul/10 -2464.0       -1
6        STANDARD LEAD USD Aug/10  -118.0        0


For each df$DESCRIPTION element containing "USD" I want to remove the last
part of it(i.e Jul/10, or Aug/10...).
I was thinking of something like that:

>Df$DESCRIPTION<-gsub("USD","new name whithout last part",df$DESCRIPTION) 

to get this following result:


                      DESCRIPTION    prix quantity
1                     CORN Jul/10    -1.5        0
2                     CORN May/10 -1082.0       -3
3      ROBUSTA COFFEE (10) Jul/10 11084.0        8
4                 SOYBEANS Jul/10  1983.5        2
5        SPCL HIGH GRADE ZINC USD -2464.0       -1
6               STANDARD LEAD USD  -118.0        0

My problem is that I have no idea how to write the regular expression in my
command line.

Any help would be appreciated.



More information about the R-help mailing list