Q:

Using gsub and paste to remove a portion of text from certain variables

I am using gsub and paste to change the columns of a dataframe. In particular, for all the columns ending by .h, where h is a number from 0 to 23 I use the following script (for example for variable p.1)
h <- 1
gsub(paste(".", h, sep = ""), "", "p.1")
# "p"          # correct!

The script should not work for all the variables that do not end with .h. For example
h <- 1
gsub(paste(".", h, sep = ""), "", "prob10")
# "pro0"          # not correct!

However, this code yields "pro0", in stead of "prob10". Similarly,
h <- 0
gsub(paste(".", h, sep = ""), "", "prob10")
# "prob"          # not correct!

gives me the wrong answer. I don't understand why gsub does not work (in first place) and why the last two examples give different results. Thank you.

A:

Well gsub works, just not as u would expect it to.
because "." is a metacharacter. 

The simplest example of a metacharacter is the full stop. 
'.' 
  The full stop character matches any single character of any sort (apart
  from a newline). 
For example, the regular expression ".at" means: any
  letter, followed by the letter 'a', followed by the letter 't'.
".at" => The cat sat on the mat .

(see https://www.stat.auckland.ac.nz/~paul/ItDT/HTML/node84.html)
The working code for you would be
for (h in 23:0) 
{gsub(paste(".", h, sep = ""), "", "p.1", fixed = TRUE)
}

