Home > Software engineering >  Give the 3 columns after column i a suffix
Give the 3 columns after column i a suffix

Time:02-25

I have a dataframe where some columnnames wouldn’t be clear if you don’t see the other columns. For example column ‘blue1’. It means the blue chair of designer Mal would cost 5 dollar.

data.frame(designer = c("mal", "didi", "yels", "don"),
           trashcan = c(1:4),
           chair = c(1:4), blue1 = c(5:8), yellow2 = c(6:9), orange3 = c(11:14),
           bedframe = c(5:8), blue4 = c(6:9), yellow5 = c(11:14), orange6 = c(16:19))
 
# designer trashcan chair blue1 yellow2 orange3 bedframe blue4 yellow5 orange6
# 1      mal        1     1     5       6      11        5     6      11      16
# 2     didi        2     2     6       7      12        6     7      12      17
# 3     yels        3     3     7       8      13        7     8      13      18
# 4      don        4     4     8       9      14        8     9      14      19
 
 
# would like to get
# designer trashcan chair blue1_chair yellow2_chair orange3_chair bedframe blue4_bedframe yellow5_bedframe orange6_bedframe
# 1      mal        1     1           5             6            11        5              6               11               16
# 2     didi        2     2           6             7            12        6              7               12               17
# 3     yels        3     3           7             8            13        7              8               13               18
# 4      don        4     4           8             9            14        8              9               14               19
 

Things to know:

  • a furniture always has 0 or 3 other colors (trashcan has 0 other colors).
  • The 3 colors always come straight after the furniture it’s about.
  • numbers behind color can be random

Any suggestions?

CodePudding user response:

We could do this in a two step in rename_with where the first one matches the 'blue', 'yellow', 'orange' followed by 1 and the second to match the same prefix followed by 2 and we paste with '_chair', '_bedframe' respectively

library(dplyr)
library(stringr)
df1 <- df1 %>% 
   rename_with(~ str_c(.x, "_chair"), matches("^(blue|yellow|orange)[0-3]$")) %>% 
   rename_with(~ str_c(.x, "_bedframe"), 
               matches("^(blue|yellow|orange)[4-6]$"))

-output

df1
 designer trashcan chair blue1_chair yellow2_chair orange3_chair bedframe blue4_bedframe yellow5_bedframe orange6_bedframe
1      mal        1     1           5             6            11        5              6               11               16
2     didi        2     2           6             7            12        6              7               12               17
3     yels        3     3           7             8            13        7              8               13               18
4      don        4     4           8             9            14        8              9               14               19

Or another option with base R

nm1 <- names(df1)[3:ncol(df1)]
i1 <- !grepl('\\d $', nm1)
i2 <- cumsum(i1)
names(df1)[-(1:2)] <- ave(nm1, i2, FUN = function(x) 
    replace(x, -1, paste0(x[-1], "_", x[1])))

-output

> df1
  designer trashcan chair blue1_chair yellow2_chair orange3_chair bedframe blue4_bedframe yellow5_bedframe orange6_bedframe
1      mal        1     1           5             6            11        5              6               11               16
2     didi        2     2           6             7            12        6              7               12               17
3     yels        3     3           7             8            13        7              8               13               18
4      don        4     4           8             9            14        8              9               14               19
  • Related