am binding a list a files together in a for loop however the verbiage and placement a of columns can be different. For example, id (in column 1) and itemID (in column 3) are the same thing but in different spots with different names. There are multiple columns like this spread among the files. Is there and way to give a list of columns names i want to change and then what i want those new column names to be. i tried using 'setnames' but it does not seem to be working. I assume because there could be a file with my desired name (itemId) and then an undesired name (tester) and then another file with vise versa (the undesired 'ID' and desired 'test')
here is an example of what i am doing:
#change names of columns from old files
tryCatch({
setnames(tempPull,
old = c("ID", "tester"),
new = c( "ItemId", "Test"))},
error = function(e){})
This is kind of a further example. The file#
are how the files looks and then the DesiredFormat
is how i would like it to look at the end. I have also provided a list of names that needed to be changed and what they should be changed to:
file1 <- data.frame(ItemId = 1:3, Test = letters[1:3])
file2 <- data.frame(ItemId = 4:7, Tester = letters[4:7])
file3 <- data.frame(ID = 7:10, Tester = letters[7:10])
file4 <- data.frame(ID = 11:12, Test = letters[11:12])
file5 <- data.frame(ID = 12:15, Testx = letters[12:15])
DesiredFormat <- data.frame(ItemId = 1:15, Test = letters[1:15])
oldnames <- c("ID", "Tester", "Testx")
newnames <- c("ItemId", "Test", "Test")