Search code examples
rreplaceradix

Replace characters in the middle of a string


I have the following df

df <- data.frame(name.name1 = c(1, 1, 1),
                 name.name2 = c(2, 2, 2),
                 name.name3 = c(3, 3, 3))

I'm trying to substitute . by space. When I do

colnames(df) <- gsub("^.$", " ", colnames(df))

What's happening here and how can I proceed?


Solution

  • You need to escape the dot with double back slash like so

    colnames(df) <- gsub("\\.", " ", colnames(df))
    

    Output:

    "name name1" "name name2" "name name3"