The column names in my .xlsx Excel sheet have spaces. How can I automatically replace the spaces with "_" or "."? I would like to use read_excel
because I need to specify a range in my excel sheet.
Remove automatically all spaces from column names using read_excel
4.1k Views Asked by nohomejerome At
4
There are 4 best solutions below
0

There is not way to tell it to do that in the method read_excel()
however you can do two things to circumvent the problem.
#create list of column names as you wish them to appear manually
names_vector<- c("name-1", "name_2"....)
#set the col_names argument equal to the list of names
data<-read_excel( <file_name>, col_names =names_vector)
These two steps will replace the header in your XLSX file with the corrected names.
0

Using this .xlsx file:
I used the library openxlsx
. Using the read.xlsx
function I got the following dataframe:
a.long.col.name yet.another.second.column
1 a 1
2 b 2
3 c 3
4 d 4
read.xlsx
also contains options to select rows and columns from the excel sheet:
read.xlsx('C:/Users/<username>/Documents/so_query.xlsx',
rows = 2:5,
cols = 2)
Gives the following dataframe:
1
1 2
2 3
3 4
0

The easiest one-liner is to add this to your read_excel
call:
, .name_repair = function(col){ gsub(" ", "_", col) }
As in:
data <- read_excel(path = excel_file,
.name_repair = function(col){ gsub(" ", "_", col) } )
In this way .name_repair
applies on the fly a simple function that uses gsub
to replace in each col
the space (" "
) with an underscore ("_"
).
Here is a way to do it using the
.name_repair
argument withread_excel()
:Creating the excel file for import:
Reading in the data several different ways: