I am unsing clear names for my "names" in my measurement data for calculations. Because of data privacy I need to anonymize the solution graphs: I therefore created an assigment table with the "clear" names and random IDs (ids)
.
randos clear
[1,] "d2ef" "01"
[2,] "6326" "02"
[3,] "fc31" "03"
[4,] "02ac" "04"
[5,] "e43a" "05"
[6,] "1ac7" "06"
how can I replace the clear by randos in my colnames()
. A colname for example is T_01_X
. The count and order may be different, because of subset, so I can not set all colnames()
just 1:1 with the assignment table. It need to be search the clear string
and replace it by the randos
.
In the End it should the names be e.g.
"01" in "T_01_X" -> "T_d2ef_X"
as well as
"01" in "A_01_Y" -> "A_d2ef_Y"
Thanks for clarifying the question. This is a good time for stringr::str_replace()
.
# Lookup table you posted
lookup <- read.table(text = "randos clear
1 d2ef 01
2 6326 02
3 fc31 03
4 02ac 04
5 e43a 05
6 1ac7 06", h=T)
# Generate some data with colnames to be replaced
dat <- data.frame(
T_01_X = 1,
T_02_X = 1,
C_G_03 = 1,
L04_B = 1,
R05Q = 1,
J06R = 1
)
names(dat) <- stringr::str_replace(
names(dat),
pattern = as.character(lookup$clear),
replacement = lookup$randos
)
dat
# T_0d2ef_X T_06326_X C_G_0fc31 L002ac_B R0e43aQ J01ac7R
# 1 1 1 1 1 1 1
EDIT: The above works for me but not for the OP. The solution which worked for the OP (see comments) is:
names(dat) <- stringi::stri_replace_all_fixed(
names(dat),
pattern = lookup$clear,
replacement = lookup$randos,
vectorize_all = FALSE
)