I'm trying to match a partial pattern of the variable names in my data set and replace them all with another pattern using gsubfn()
.
I'm using R version 4.0.3 (2020-10-10).
The below code shows the sample pattern of variable names in the data set and how I tried to replace them
replace_str = c("Race..American.India", "Race.White")
gsubd_str = gsubfn(pattern = "Race..| Race.", "R_", x = replace_str)
When I used the pattern string as above, my output is:
> gsubd_str
[1] "R_American.India" "R_hite"
However, if I use (I changed the order of pattern to match):
gsubd_str = gsubfn(pattern = "Race.| Race..", "R_", x = replace_str)
then my output is:
gsubd_str
[1] "R_.American.India" "R_White"
In both the cases, my thoughts are that gsubfn()
is not behaving as expected.
At least in the second case, gsubfn()
replaced the variable as soon as the LHS of "|"
was TRUE
.
However, in the first case, after the match was found, gsubfn()
replaced 3 characters "R"
, "."
, "W"
instead of 2, "R"
and "."
.
Not sure if I understood gsubfun()
correctly.