1

I want to iteratively assign a vector of strings to the names (as in column names of a matrix) of the objects they represent. Example:

> Names
[1] "gs2"  "gs3"  "gs5"  "gs7"  "gs10"

The objects are xts timeseries of interest rates, for example:

> head(gs2,3)
           gs2Day
1976-06-01 0.0702
1976-07-01 0.0674
1976-08-01 0.0650
> class(gs2)
[1] "xts" "zoo"

So for this timeseries object I want to change the name from "gs2Day" to "gs2", but I want to do it iteratively over many time series. Something like this would be nice in a for loop (I know it's considered bad form and could use an apply instead),

> names(noquote(Names)[i]) = Names[i]

but of course it doesn't work. I've tried many approaches and none work. I suppose I could merge them into a matrix and iteratively name the columns, then strip off the columns into individual timeseries, but this seems rather crude.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
eigenvalet
  • 33
  • 5
  • 1
    Try `lst <- mget(Names);lapply(names(lst), function(x) {x1 <- lst[[x]]; colnames(x1) <- x; x1 })` – akrun Jul 31 '15 at 20:31
  • @akrun: why not as an answer? I know there _might_ be a duplicate, but `mget` is fairly high level R, even is it seems obvious to us. – IRTFM Aug 01 '15 at 03:41
  • @BondedDust Thanks for the comment. I was waiting for the OP to respond as I didn't test the code. – akrun Aug 01 '15 at 07:15
  • Thanks akrun for the idea but it didn't do what I needed. This works: – eigenvalet Aug 05 '15 at 11:49
  • I can't insert code in the reply comment - get error message: "too long by 245 characters". Searched for solution without success. Any suggestions of how to reply with code? Tried cntrl-k and surrounding code with backticks. – eigenvalet Aug 05 '15 at 12:07
  • I moved the answer you edited into your question to a community-wiki answer that you can accept. That way people won't think this is still an open question. I also removed all the transformations you included in your answer, since the functions used were proprietary and the specific transformations were not relevant to your question. – Joshua Ulrich Aug 17 '15 at 17:20

1 Answers1

0

Here's what the OP says worked for them, even though it is not reproducible:

fredTable <-
structure(list(Symbol = structure(c(3L, 1L, 4L, 2L, 5L),
  .Label = c("CASACBM027SBOG", "FRPACBW027SBOG", "TLAACBM027SBOG", "TOTBKCR",
  "USNIM"), class = "factor"), Name = structure(1:5, .Label = c("bankAssets",
  "bankCash", "bankCredWk", "bankFFRRPWk", "bankIntMargQtr"), class = "factor"),
  Category = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Banks", class = "factor"),
  Country = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "USA", class = "factor"),
  Lead = structure(c(1L, 1L, 3L, 3L, 2L), .Label = c("Monthly", "Quarterly",
  "Weekly"), class = "factor"), Freq = structure(c(2L, 1L, 3L, 3L, 4L),
  .Label = c("1947-01-01", "1973-01-01", "1973-01-03", "1984-01-01"),
  class = "factor"), Start = structure(c(1L, 1L, 1L, 1L, 1L), .Label = "Current",
  class = "factor"), End = c(TRUE, TRUE, TRUE, TRUE, FALSE), SeasAdj = c(FALSE,
  FALSE, FALSE, FALSE, TRUE), Percent = structure(c(1L, 1L, 1L, 1L, 1L),
  .Label = "Fed", class = "factor"), Source = structure(c(1L, 1L, 1L, 1L, 1L),
  .Label = "Res", class = "factor"), Series = structure(c(1L, 1L, 1L, 1L, 2L),
  .Label = c("Level", "Ratio"), class = "factor")), .Names = c("Symbol", "Name", 
  "Category", "Country", "Lead", "Freq", "Start", "End", "SeasAdj", "Percent",
  "Source", "Series"), class = "data.frame",
  row.names = c("1", "2", "3", "4", "5"))
fredNamesOrig <- fredTable$Name     # original downloaded FRED object names
fredObjOrig <- mget(fredNamesOrig)  # list of original FRED objects
fredNamesTrans <- fredNamesOrig     # pre-allocate list of transformed objects
# Strip off period string ("Day", "Qtr")
for(i in 1:length(fredNamesOrig)){
    if(grepl("Day",fredNamesOrig[i]))
        fredNamesTrans[i] <- sub("Day","",fredNamesOrig[i])
    else if(grepl("Qtr",fredNamesOrig[i]))
        fredNamesTrans[i] <- sub("Qtr","",fredNamesOrig[i])
}

# Assign transformed series back to appropriately names series:
for(i in 1:length(fredObjOrig)){
  colnames(fredObjOrig[[i]]) <- fredNamesTrans[i]
  assign(fredNamesTrans[i],fredObjOrig[[i]])
}
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418