2

I have data frame like this

> d <- data.frame(team.aaa=1:3, team.aab=4:6, team.aac=7:9)
> d

#   team.aaa team.aab team.aac
#1        1        4        7
#2        2        5        8

and, desired output

d <- rename(d, c("team.aaa"="aaa_team", "team.aab"="aab_team", "team.aac"="aac_team"))
> d
#  aaa_team aab_team aac_team
#1        1        4        7
#2        2        5        8
#3        3        6        9

I could do it with rename string, but want to use search and replace option because of huge data volume Many thanks in advance

zx8754
  • 52,746
  • 12
  • 114
  • 209
Jay
  • 67
  • 8

1 Answers1

2

Based on the example showed in the OP's post, it seems that the suffix part after the . should be the prefix and viceversa. In that case, we can use sub to capture the characters that are not a . followed by ., then capture the rest of the characters ((.*)) and replace it with the backreference arranged accordingly.

names(d) <- sub("([^.]+).(.*)", "\\2_\\1", names(d))
names(d)
#[1] "aaa_team" "aab_team" "aac_team"

Or another option would be to split the string by . and then paste after reversing the order

sapply(strsplit(names(d), "[.]"), function(x) paste(rev(x), collapse="_"))

Or as @jota mentioned in the comments, if 'team' is always the first word, we can make it more compact with sub

sub("team\\.(.*)", "\\1_team", names(d)) 
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you very much – Jay Jun 16 '16 at 04:01
  • 1
    If "team" is always the first word: `gsub("team\\.(.*)", "\\1_team", names(d), perl = TRUE)` – Jota Jun 16 '16 at 04:12
  • @ jota Thanks very much as you mentioned "team" is not always first word, I have some columns with names like "build_MS2_aab" my desired output will be "aab_build_MS2". Many thanks in advance – Jay Jun 16 '16 at 15:48