Home > OS >  Change single column name in several data.frame objects in R
Change single column name in several data.frame objects in R

Time:09-23

I regularly get several xts objects with dates as row names. I have to transform those xts objects into data.frame objects, but when I do that, the row names as date become a new 1st column named Ìndex containing the dates. But I need those Index columns to be called Date.

Therefore, I want to iteratively change the 1st column names of all the newly converted data.frame objects from Index to Date.

However, when I try the (reproducible) code below, at point 4, I systematically get an Error in `colnames<-`(`*tmp*`, value = `*vtmp*`) : attempt to set 'colnames' on an object with less than two dimensions

#**************#
# 1. Load data #
#**************#
library(fs)
library(quantmod)
library(zoo)
tickers <- c("NKLA", "MPNGF", "RMO", "JD", "COIN")
getSymbols.yahoo(tickers, auto.assign = TRUE, env = globalenv())
closeAllConnections()


#******************************#
# 2. Find all loaded xts files #
#******************************#
xtsObjects <- names(which(unlist(eapply(.GlobalEnv, is.xts))))


#******************************************************#
# 3. Convert into data.frames found xts files under 2. #
#******************************************************#
for (i in seq_along(xtsObjects)) {
  assign(xtsObjects[i], fortify.zoo(get(xtsObjects[i])))
}


#***************************************************************#
# 4. Change name of 1st column to Date of converted xts objects #
#***************************************************************#

# Check if for loop prints what I want, as a precaution
for (i in seq_along(xtsObjects)) {
  print(get(xtsObjects[i]))
}

# But when I try the following, I get the error shown above
for (i in seq_along(xtsObjects)) {
  colnames(xtsObjects[i])[1] <- "Date"
}

# Same error here
for (i in seq_along(xtsObjects)) {
  colnames(xtsObjects)[which(names(xtsObjects) == "Index")] <- "Date"
}

# Still same error
for (i in seq_along(xtsObjects)) {
  colnames(xtsObjects)[colnames(xtsObjects) == "Index"] <- "Date"
}

My question:

  • What loop code do I have to type in order to change the 1st column named Index to Date in all freshly converted xts objects into data.frame objects?

Systems used:

  • R version: 4.1.1 (2021-08-10)
  • RStudio version: 1.4.1717
  • OS: macOS Catalina version 10.15.7 and macOS Big Sur version 11.6

CodePudding user response:

We need to get the value and then do the assign

for (i in seq_along(xtsObjects)) {
     tmp <- get(xtsObjects[i])
      colnames(tmp)[colnames(tmp) == "Index"] <- "Date"
      assign(xtsObjects[i], tmp)
}

Now, check the column names

> lapply(mget(xtsObjects), names)
$MPNGF
[1] "Date"           "MPNGF.Open"     "MPNGF.High"     "MPNGF.Low"      "MPNGF.Close"    "MPNGF.Volume"   "MPNGF.Adjusted"

$NKLA
[1] "Date"          "NKLA.Open"     "NKLA.High"     "NKLA.Low"      "NKLA.Close"    "NKLA.Volume"   "NKLA.Adjusted"

$JD
[1] "Date"        "JD.Open"     "JD.High"     "JD.Low"      "JD.Close"    "JD.Volume"   "JD.Adjusted"

$COIN
[1] "Date"          "COIN.Open"     "COIN.High"     "COIN.Low"      "COIN.Close"    "COIN.Volume"   "COIN.Adjusted"

$RMO
[1] "Date"         "RMO.Open"     "RMO.High"     "RMO.Low"      "RMO.Close"    "RMO.Volume"   "RMO.Adjusted"
  • Related