Change letter case of column names

rename column names
change column name in r
convert a column to uppercase in r
r capitalize first letter
dplyr rename all columns
convert column to lowercase r
dplyr rename all columns lowercase
how to change the name of a column in a dataset in r

I have a large number of data sets each containing a long list of column names. In some files the column names are all capital letters and in some files only the first letter of the column names is capitalized. I need to append the data sets and thought the easiest way to match column names among data sets would be to convert the all-capital names into names with only the first letter capitalized.

I am hoping to find a general solution, maybe even a one-liner.

Here is my example data set. The desired names are included in the names statements.

my.data2 <-  "
landuse units grade CLAY    LINCOLN  BASINANDRANGE  MCCARTNEY  MAPLE
apple   acres AAA     0         2          3             4         6
apple   acres AA   1000       900         NA            NA       700
pear    acres AA   10.0        20         NA          30.0        40
peach   acres AAA   500       400        350           300       200
"
my.data2 <- read.table(textConnection(my.data2), header=TRUE)

names(my.data2)[names(my.data2)=="CLAY"]            <- "Clay"
names(my.data2)[names(my.data2)=="BASINANDRANGE"]   <- "BasinandRange"
names(my.data2)[names(my.data2)=="LINCOLN"]         <- "Lincoln"
names(my.data2)[names(my.data2)=="MCCARTNEY"]       <- "McCartney"
names(my.data2)[names(my.data2)=="MAPLE"]           <- "Maple"

my.data2

Note that I included the names McCartney and BasinandRange to make things more realistic and more difficult. However, if I can find a one-liner to deal with 95% of the names and use the above names statements to deal with complications like McCartney and BasinandRange that would be great.

I have searched the internet, including the StackOverflow archives, without finding a solution. Sorry if I overlooked one. Thank you for any help.


Here is a one-liner implementing "the easiest way to match column names among data sets" that I can think of:

## Columns 1:3 left unaltered since they are not place names.
names(my.data2)[-1:-3] <- tolower(names(my.data2)[-1:-3])

## View the results
names(my.data2)
# [1] "landuse"       "units"         "grade"         "clay"         
# [5] "lincoln"       "basinandrange" "mccartney"     "maple"   

Change letter case of column names, In the example below, the PROPER function is used to convert the uppercase names in column A to proper case, which capitalizes only the first letter in each  How To Change Case In Excel: Capitalize First (Or ALL) Letters + More! Written by co-founder Kasper Langmann, Microsoft Office Specialist. There are times when letter case in Excel needs to be changed or corrected.


data.table syntax, I believe would save more time and efficient. its also a one line statement, even shorter.

library(data.table)
setnames(my.data2, tolower(names(my.data2[4:8])))

#   landuse units grade clay lincoln basinandrange mccartney maple
#1:   apple acres   AAA    0       2             3         4     6
#2:   apple acres    AA 1000     900            NA        NA   700
#3:    pear acres    AA   10      20            NA        30    40
#4:   peach acres   AAA  500     400           350       300   200

Change the case of text - Office Support, In the example below, the PROPER function is used to convert the uppercase names in column A to proper case, which capitalizes only the first letter in each  When you use sp_rename, the new column name is literally, treating the square brackets as part of the column name, which I had overlooked. So, you should change the "QUOTENAME(LOWER(COLUMN_NAME))" to LOWER(COLUMN_NAME).


modern solution

This is now a job for janitor::clean_names(), just choose case parameter that fits you need.

Change the case of text - Office Support, Import modules import pandas as pd # Set ipython's max row display pd.​set_option('display.max_row', 1000) # Set iPython's max column width  In some files the column names are all capital letters and in some files only the first letter of the column names is capitalized. I need to append the data sets and thought the easiest way to match column names among data sets would be to convert the all-capital names into names with only the first letter capitalized.


I used Josh O'Brien's answer, but eventually wrote the code below that creates column names with the first letter in upper case and the other letters in lower case, with a few exceptions handled as in the original post. Below I used the same data set as in the original post, but read that data into R differently where n.col determines the number of columns in the data file:

n.col <- as.numeric(length(scan("c:/users/mark w miller/simple R programs/names_with_capital_letters.txt", 
         what="character", nlines=1)))

my.data2 <- read.table(file = "c:/users/mark w miller/simple R programs/names_with_capital_letters.txt", 
            na.string=NA, header = T, colClasses = c('character', 'character', 'character', 
            rep('numeric', (n.col[1] - 3))))

first.letter  <- substring(names(my.data2)[-1:-3], 1, 1)
other.letters <- tolower(substring(names(my.data2)[-1:-3], 2))
newnames      <- paste(first.letter, other.letters, sep="")

names(my.data2)[-1:-3] <- newnames
names(my.data2)[names(my.data2)=="Basinandrange"]   <- "BasinandRange"
names(my.data2)[names(my.data2)=="Mccartney"]       <- "McCartney"

my.data2

#   landuse units grade Clay Lincoln BasinandRange McCartney Maple
# 1   apple acres   AAA    0       2             3         4     6
# 2   apple acres    AA 1000     900            NA        NA   700
# 3    pear acres    AA   10      20            NA        30    40
# 4   peach acres   AAA  500     400           350       300   200

Lower Case Column Names In Pandas Dataframe, I am trying to convert a column from lower case to upper case, but it is not working. My code: #Change all city name in city to uppercase. library(magrittr)  Type in a series of names or text in a column in your worksheet. Your text can be in any case when using this function. It will change the text in the cell to all uppercase letters.


A "tidy" solution:

library(dplyr)

my.data2.mod <- my.data2 %>% 
  rename_at(c("CLAY", "LINCOLN", "BASINANDRANGE", "MCCARTNEY",  "MAPLE"),
            .funs = tolower)

names(my.data2.mod) 
# [1] "landuse"       "units"         "grade"         "clay"         
# [5] "lincoln"       "basinandrange" "mccartney"     "maple"   

Also, to answer the original question and leave some cases capitalized, you can use the snakecase package:

library(snakecase)

my.data2.mod = my.data2 %>% 
  rename_at(
    c("CLAY", "LINCOLN", "BASINANDRANGE", "MCCARTNEY",  "MAPLE"),
    .funs = list(
      ~ to_upper_camel_case(., 
                            abbreviations = c("McCartney", "BasinandRange")
                            )
      )
    )

names(my.data2.mod)
# [1] "landuse"       "units"         "grade"         "Clay"         
# [5] "Lincoln"       "BasinandRange" "McCartney"     "Maple" 

Converting lower case to upper case, This function loops over the objects in an environment. It checks if it is a data frame. Then it changes the column names to uppercase. How to lowercase column names in Pandas dataframe Analyzing real-world data is somewhat difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required format is also very important.


Changing all data frame column names to upper case in R, Python | Change column names and row indexes in Pandas DataFrame · Shivam_k. Check out this Author's contributed articles. If you like GeeksforGeeks and  You cannot change the row names, or numbering, but you can add your desired row names in column A for the corresponding rows. Instead, if you want to have meaningful column header names, you can do the following. Click in the first row of the worksheet and insert a new row above that first row.


Apply uppercase to a column in Pandas dataframe, You have a list of names and it's all lower case letter. We can change text to lower case, upper case or proper case where each This will create a new column with all text converted to lower case letters using the Text. Easy: Select the names in the column you just auto-filled, then click Copy or press CTRL + C. Right-click in the beginning column; in my case, it’s A3. Click the Paste Special menu for


5 Ways To Change Text Case In Excel, Tests are also performed so that all column names will stay unique! df is returned unchanged, except that capital letters in names are changed to lower case. Lower Case Column Names In Pandas Dataframe. 20 Dec 2017 . Preliminaries # Import modules import pandas as pd # Set ipython's max row display pd. set_option ('display.max_row', 1000) # Set iPython's max column width to 50 pd. set_option ('display.max_colu