0

Suppose the next dataframe:

df <- data.frame("level.3.london"=seq(1,5), "level.2.bogota"=seq(1,5), "level"=seq(1,5))

Is it possible to replace . for an space over the dataframe column names? The idea is to get the next output:

names(df)

Console output:

[1] "level 3 london" "level 2 bogota" "level"  
TheFunSideofData
  • 1,284
  • 1
  • 11
  • 26

1 Answers1

3

A regular expression and the names function should do what you want, but be warned that column names with spaces can be difficult to work with.

names(df) <- gsub('\\.', ' ', names(df))

If you want to reference those column names, you will need to enclose them in backticks:

df$`level 3 London`
jdobres
  • 10,107
  • 1
  • 15
  • 35