'Give the 3 columns after column i a suffix

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source