'What is the command to create another column of colour codes to assign the values of the other column?

I have a column named Material code which contains different and some repeated values. I want to create another column containing colour codes which assign to the values in the Material code column.

Is there a command which facilitates this process?

This is the colour palette of 20 different colours as in the Material code column there are 20 different values.

["#e45ee0",
"#59ab03",
"#0079f1",
"#a5d32c",
"#96167c",
"#7bdc74",
"#d6a0ff",
"#01a068",
"#dd5609",
"#01afe1",
"#903429",
"#1f5392",
"#cbcb74",
"#763c7c",
"#6a4e0e",
"#ecb2f8",
"#565485",
"#ff7699",
"#ad6e8d",
"#ff91d2"]

The column material code:

URI
TRT
TRT
TRT
CVC
SVP
TRT
TRT
BAL
TRT
TRT
TRT
SCVGSX
BAL
TRT
BAL
BAL
TRT
BAL
TRT
BAL
TRT
TRT
BAL
BAL
TRT
TRT
TRT
BALSX
TRT
TRT
URI
TRT
TRT
BAL
SCVFDX
BAL
TRT
SCVFSX
TRT
SCVGSX
BAL
TRT
SCVGSX
BAL
TRT
SVP
TRT
TRT
SCA
TRT
SCARSX
SCARSX
BAS
TRT
SCARSX
TRT
BAL
BAL
SCVC
TNADX
SVP
BAL
SCDGSX
TRT
BALSX
BAL
SCVGDX
BAL
TRT
BAL
TRT
BAL
TRT
TRT
SCA
BAL
SCAFDX
TRT
BAL
URI
TRT
SCVGSX
URI
BAL
TRT
SCVGDX
TRT
BAL
BAL
CVGSX
TRT
BAS
TRT
TRT
BAL
SCVFDX
TRT
BAS
TRT
TRT
BAL
TRT
BAL
BAL
TRT
SAN
SAN


Solution 1:[1]

You have tagged your question with both r and python, so it's not clear which you prefer.

There are only 19 unique values in the Material column you provided, but if we take just the first 19 colours, in R we can do:

as.character(factor(Material, labels = colour[1:19]))
#>   [1] "#ad6e8d" "#ff7699" "#ff7699" "#ff7699" "#a5d32c" "#ecb2f8" "#ff7699"
#>   [8] "#ff7699" "#e45ee0" "#ff7699" "#ff7699" "#ff7699" "#6a4e0e" "#e45ee0"
#>  [15] "#ff7699" "#e45ee0" "#e45ee0" "#ff7699" "#e45ee0" "#ff7699" "#e45ee0"
#>  [22] "#ff7699" "#ff7699" "#e45ee0" "#e45ee0" "#ff7699" "#ff7699" "#ff7699"
#>  [29] "#59ab03" "#ff7699" "#ff7699" "#ad6e8d" "#ff7699" "#ff7699" "#e45ee0"
#>  [36] "#1f5392" "#e45ee0" "#ff7699" "#cbcb74" "#ff7699" "#6a4e0e" "#e45ee0"
#>  [43] "#ff7699" "#6a4e0e" "#e45ee0" "#ff7699" "#ecb2f8" "#ff7699" "#ff7699"
#>  [50] "#d6a0ff" "#ff7699" "#dd5609" "#dd5609" "#0079f1" "#ff7699" "#dd5609"
#>  [57] "#ff7699" "#e45ee0" "#e45ee0" "#903429" "#565485" "#ecb2f8" "#e45ee0"
#>  [64] "#01afe1" "#ff7699" "#59ab03" "#e45ee0" "#763c7c" "#e45ee0" "#ff7699"
#>  [71] "#e45ee0" "#ff7699" "#e45ee0" "#ff7699" "#ff7699" "#d6a0ff" "#e45ee0"
#>  [78] "#01a068" "#ff7699" "#e45ee0" "#ad6e8d" "#ff7699" "#6a4e0e" "#ad6e8d"
#>  [85] "#e45ee0" "#ff7699" "#763c7c" "#ff7699" "#e45ee0" "#e45ee0" "#96167c"
#>  [92] "#ff7699" "#0079f1" "#ff7699" "#ff7699" "#e45ee0" "#1f5392" "#ff7699"
#>  [99] "#0079f1" "#ff7699" "#ff7699" "#e45ee0" "#ff7699" "#e45ee0" "#e45ee0"
#> [106] "#ff7699" "#7bdc74" "#7bdc74 "

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Allan Cameron