user2786437
user2786437

Reputation: 53

Replace a value in a column matching a particular pattern in R

I am new to R and i am not able to find how to do this.

Consider a table named Trial like this

  Col1  Col2            Col3 
   1    hey    June 24, 2013 5:15:06 PM GMT+05:30
   2    hello  april 20, 2013 5:15:06 PM GMT+05:30
   3    hey    July 23, 2012 5:15:06 PM GMT+05:30

How do i replace values of Col3 in Trial to

  Col1  Col2       Col3 
   1    hey      June 2013 
   2    hello    april 2013 
   3    hey      July 2012

ie.i need to extract only month and year and make a new column or replace in the same. Thnx in advance!

Upvotes: 3

Views: 600

Answers (1)

agstudy
agstudy

Reputation: 121568

Use this for example:

 dat$Col3 <- gsub('(^\\w+).*, ([0-9]{4}).*','\\1 \\2',dat$Col3)

Upvotes: 1

Related Questions