How to prevent dbReadTable from changing column names

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to prevent dbReadTable from changing column names



I am using the DBI package to read data from a SQL Server database. The function dbReadTable automatically changes the columns names to replace spaces with dots.


DBI


dbReadTable



Is there a way to control what get replace by what and or to not change anything?



The reason for this is, earlier I was using RODBC package with sqlQuery function which does not change the column names and there is a lot of code already written with old column names.


RODBC


sqlQuery




1 Answer
1



This really depends on the DBI backend you're using. You could try:


dbReadTable(..., check.names = FALSE)



If you need a custom renaming strategy (e.g. replacing spaces by underscores) you need to read in with names unchanged and implement the renaming yourself:


clean_names <- function(x)
gsub(" ", "_", x)


data <- dbReadTable(.......)
names(data) <- clean_names(names(data))





Thanks! This works for keeping the column names intact but can I control the replacement for space? Also what does DBI backend mean?
– Ashish Singhal
Aug 13 at 14:41





Can you please rephrase "replacement for space"? See r-dbi.org for a very brief overview.
– krlmlr
Aug 13 at 18:26





"replacement for space": by default dbReadTable replaces a space in the column name with a dot. I want to replace the space in column name by an underscore "_" instead.
– Ashish Singhal
Aug 13 at 20:58





@AshishSinghal: Edited answer.
– krlmlr
Aug 13 at 21:53






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered