Yan Song
Yan Song

Reputation: 110

string manipulations using R

I am working with a string vector.Each element in the vector is in the format "MMYYPub". I want to switch the place of "MM" and "YY" in the string, from "MMYYPub" to "YYMMPub" . Is this feasible in R. Example :

 vec(1)
'0100pub'
 vec(2)
'0200pub'

Where the first two digits are month, and the following digits are year. There 10 years data in total from 1994 to 2013.

Upvotes: 0

Views: 105

Answers (4)

Henrik
Henrik

Reputation: 67778

It might also be useful to know about the yearmon class to represent monthly data. The yearmon object can then printed in a format of choice.

library(zoo)
ym <- as.yearmon("0414pub", format = "%m%ypub")
ym
# [1] "apr 2014"

format(ym, "%y%mpub")
# [1] "1404pub"

Upvotes: 5

Matthew Lundberg
Matthew Lundberg

Reputation: 42629

You might not need regex for this, if you just want to swap the characters around. substring and paste work just as well in this case:

> f <- function(x) paste0(substring(x,3,4), substring(x,1,2), substring(x,5))
> x
[1] "0103pub" "0204pub"
> f(x)
[1] "0301pub" "0402pub"

Upvotes: 1

Dirk is no longer here
Dirk is no longer here

Reputation: 368191

You need to read up on regular expressions. Here is one way:

R> val <- "0405pub"
R> gsub("(\\d\\d)(\\d\\d)(.*)", "\\2\\1\\3", val)
[1] "0504pub"
R> 

We use the fact that

  • \d denotes a digit (but need to escape the backslash)
  • (...) groups arguments, so here we matches one (two digits), two (also two digits) and three (remainder)
  • we then "simply" create the replacement string as "two before one followed by three"

There are other ways to do it, this will accomplish given the pattern you described.

Edit: Here is a shorter variant using \\d{2} to request two digits:

R> gsub("(\\d{2})(\\d{2})", "\\2\\1", val)
[1] "0504pub"
R> 

Upvotes: 4

hwnd
hwnd

Reputation: 70722

One way would be using (gsub) to replace all occurrences in the vector.

> vec <- c('0100pub', '0200pub')
> gsub('([0-9]{2})([0-9]{2})', '\\2\\1', vec)
[1] "0001pub" "0002pub"

Upvotes: 4

Related Questions