0

I have a data set that has multiple patterned column names, "tstpos_012720", and "pbpos_012720" basically one for each date. I want to rename them in such a way that I can have each pair ordered together. I am looking for something where I can move the date to the front and add a "t" or "p" to the end. Such that I go from "tstpos_012720, pbpos_012720" to "012720_t, 012720_p"

I am fairly new to R, but I couldn't find anything similar.

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 07 '21 at 03:29
  • You can also use the regex `names(df) <- sub('(.).*_(\\d+)', '\\2_\\1', names(df))` – Ronak Shah Jul 07 '21 at 03:48

1 Answers1

1

With the values

x <- c("tstpos_012720", "pbpos_012720")

You can use gsub which takes a regular expression. You can use matching groups to extract and rearrange the parts you are interested in.

gsub("(.)[^_]*_(.*)", "\\2_\\1", x)
# [1] "012720_t" "012720_p"

for column names, you can do

names(mydata) <- gsub("(.)[^_]*_(.*)", "\\2_\\1", names(mydata))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • I guess I should clarify a bit. The data frame I am working with has hundreds of columns repeating that pattern. When I enter `gsub("(.)[^_]*_(.*)", "\\2_\\1", WA_Cases)` it fails to rename the columns. I am sure I am missing something. – Jacob Ritter Jul 07 '21 at 04:00
  • Thank you so much. That second part did the trick. – Jacob Ritter Jul 07 '21 at 04:07