1

I've checked the other questions related to this issue but none of the solutions worked (Changing column names of a data frame, Renaming column names in Pandas)

I have a data frame where I use indexes to merge data frames together. That works fine, except the new column headers on the data frame likes to double the name in the new column. For example: I expect the new column in the main data frame to say "LiveDemand" but instead it says "LiveDemand$LiveDemand".

I then try to use colnames() to replace the column header to a new name. I get no errors, but when I check the data frame the name is unchanged. Can anyone help explain why? Here is my code:

mainDF$LiveDemand <- DemandDF[DemandIndexes, "LiveDemand"]
#Result is a new column called 'LiveDemand$LiveDemand'
colnames(mainDF)[which(names(mainDF) == "LiveDemand$LiveDemand")] <- "LiveDemand"
#I've also tried
colnames(mainDF)[18] <- "LiveDemand"
#This as well
mainDF<- rename(mainDF, LiveDemand= "LiveDemand")

All three methods give no errors, but the column header doesn't change (none of them do). What is really strange is when I use rename, if I say the old column name is "LiveDemand$LiveDemand" it gives me an error saying it doesn't exist, but when I go to view the data frame, it clearly says "LiveDemand$LiveDemand". Any ideas?

M--
  • 25,431
  • 8
  • 61
  • 93
Steven
  • 150
  • 1
  • 2
  • 14
  • 3
    CAn you do `mainDF$LiveDemand <- DemandDF$LiveDemand[DemandIndexes]` I am assuming that either your dataset is tibble or data.table. – akrun Apr 06 '23 at 15:56
  • 3
    Making this reproducible would be good, could you share `dput(mainDF[1:3, ])`? – Gregor Thomas Apr 06 '23 at 16:00
  • try surrounding the name that inlcudes `$` with back ticks: `\`LiveDemand$LiveDemand\`` instead of quotation marks `"LiveDemand$LiveDemand"` – Paul Stafford Allen Apr 06 '23 at 16:17
  • Thank you Akrun! That worked around the columns from even appearing with a doubling name. I still find it odd that I couldn't replace their names like normal column names could be; however, now it's a non-issue so that works too! – Steven Apr 06 '23 at 16:24
  • @Steven It depens on the class of your data. If it is a data.frame, then `DemandDF[DemandIndexes, "LiveDemand"]` would coerce to a vector as drop = TRUE is by default with `[`, whereas in tibble or data.table, it still returns a tibble/data.table with a single column. – akrun Apr 06 '23 at 16:28

1 Answers1

1

It may be better to directly extract the column as vector with $ and then subset using the index, thus avoiding the drop argument value (which is TRUE - for data.frame and FALSE for data.table/tibble - i.e. it coerce to a vector when there is a single row or single column in data.frame, whereas in tibble, data.table, it will still be a tibble/data.table with a single column)

mainDF$LiveDemand <- DemandDF$LiveDemand[DemandIndexes]
akrun
  • 874,273
  • 37
  • 540
  • 662