rstringdataframedatetimestring-substitution

Character joining and deleting in R


I have a data and it has a column yearmonthweek. For example, 2012y05m1wk means 1st week of May, 2012. Likewise, 2012y11m3wk means 3rd week of November, 2012. data looks like this:

data<-data.frame(yearmonthweek=c("2012y05m1wk","2012y05m2wk","2012y05m4wk","2012y11m3wk"))

Here, I want to change the expression as below:

data<-data.frame(yearmonthweek=c("2012051","2012052","2012054","2012113"))

How can I do?


Solution

  • Remove the letters from the column could work:

    gsub('[a-z]','',data$yearmonthweek)