I am trying to make models output prettier with pre-defined labels for my variables. I have a vector of variable names (a), a vector of labels (b) and model terms (c).
I have to match the vectors (a) and (c) and replace (a) by (b). I found this question that introduced me to the function gsubfn
from the package library(gsubfn)
. The function match and replace multiple strings. Following their example, it did not work properly in my case:
library(gsubfn)
a <- c("ecog.ps", "resid.ds", "rx")
b <- c("ECOG-PS", "Residual Disease", "Treatment")
c <- c("ecog.psII", "rxt2", "ecog.psII:rxt2")
gsubfn("\\S+", setNames(as.list(b), a), c)
[1] "ecog.psII" "rxt2" "ecog.psII:rxt2"
If I use a specific pattern, then it works:
gsubfn("ecog.ps", setNames(as.list(b), a), c)
[1] "ECOG-PSII" "rxt2" "ECOG-PSII:rxt2"
So I guess my problem is the regular expression used as the argument pattern in the function gsubfn
. I checked this R-pub, and Hadley's book for regular expressions. It seems that \S+
is adequate. I tried other regular expressions without success:
gsubfn("[:graph:]", setNames(as.list(b), a), c)
[1] "ecog.psII" "rxt2" "ecog.psII:rxt2"
gsubfn("[:print:]", setNames(as.list(b), a), c)
[1] "ecog.psII" "rxt2" "ecog.psII:rxt2"
Which pattern should be used in the function gsubfn
to match the vectors (a) and (c) and replace (a) by (b)?