4

If I want to change the name from 2 column to the end , why my command does not work ?

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"), row.names = c("1", "2", "3", 
"4", "5"), class = "data.frame")

Then in order to change the second column name to the end I use the following order but does not work

names(fredTable[,-1]) = paste("case", 1:ncol(fredTable[,-1]), sep = "")

or

names(fredTable)[,-1] = paste("case", 1:ncol(fredTable)[,-1], sep = "")

In general how one can change column names of specific columns for example 2 to end, 2 to 7 and etc and set it as the name s/he like

  • subset on the outside instead of within the function. As in `names(x)[-1]` instead of `names(x[-1])`. For more here's a link http://stackoverflow.com/questions/6081439/changing-column-names-of-a-data-frame-in-r – Pierre L Feb 25 '16 at 16:29
  • @Pierre Lafortune can you please give me a complete answer. I tried that and I get error Error in 1:ncol(fredTable)[-1] : argument of length 0 . Then I tried names(fredTable)[-1] = paste("case", 1:ncol(fredTable), sep = "_") and again I got error Warning message: In names(dfm)[-1] = paste("Fraction", 1:ncol(dfm), sep = "_") : number of items to replace is not a multiple of replacement length – koskesh kiramtodahanet Feb 25 '16 at 16:34

1 Answers1

4

Replace specific column names by subsetting on the outside of the function, not within the names function as in your first attempt:

> names(fredTable)[-1] <- paste("case", 1:ncol(fredTable[,-1]), sep = "")

Explanation

If we save the new names in a vector newnames we can investigate what is going on under the hood with replacement functions.

#These are the names that will replace the old names
newnames <- paste("case", 1:ncol(fredTable[,-1]), sep = "")

We should always replace specific column names with the format:

#The right way to replace the second name only
names(df)[2] <- "newvalue"

#The wrong way
names(df[2]) <- "newvalue"

The problem is that you are attempting to create a new vector of column names then assign the output to the data frame. These two operations are simultaneously completed in the correct replacement.

The right way [Internal]

We can expand the function call with:

#We enter this:
names(fredTable)[-1] <- newnames

#This is carried out on the inside
`names<-`(fredTable, `[<-`(names(fredTable), -1, newnames)) 

The wrong way [Internal]

The internals of replacement the wrong way are like this:

#Wrong way
names(fredTable[-1]) <- newnames

#Wrong way Internal
`names<-`(fredTable[-1], newnames)

Notice that there is no `[<-` assignment. The subsetted data frame fredTable[-1] does not exist in the global environment so no assignment for `names<-` occurs.

Pierre L
  • 28,203
  • 6
  • 47
  • 69
  • great! can you please also make it for when we want to select part of the column names like column 2 to 7. this way we cover all type of column names changes which can be used latter with other users . I liked your answer and I accepted it. so please change it to make it more general – koskesh kiramtodahanet Feb 25 '16 at 16:44