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: