Q:

How to convert a letter in a string to a number in R

I have a data set with a column of values V01, V02, V03,...,V91 among other values such as E000, E001, 402 , and so on. How can I replace the V values with a number like 1000 in this column without converting the column to a vector?
(I can't use the function as.numeric since I don't want my "E..." values become NA and get mixed with the "V..." values.)
Example:
my data looks like:
{V01
527
E54
64.5}
I want it to look like:
{100001
527
E54
64.5}
Thank you!

A:

You can use gsub:
x <- c("V01","527","E54", "64.5")
gsub("V","1000",x)

[1] "100001" "527"    "E54"    "64.5" 

EDIT To apply on one column:
df <- data.frame(col1=c("V01","527","E54", "64.5"),col2=2)
df$col1 <- gsub("V","1000",df$col1 )
df
    col1 col2
1 100001    2
2    527    2
3    E54    2
4   64.5    2

