Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change the names for certain columns in a data frame [duplicate]

Tags:

r

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

like image 692
koskesh kiramtodahanet Avatar asked Sep 13 '25 06:09

koskesh kiramtodahanet


1 Answers

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.

like image 57
Pierre L Avatar answered Sep 14 '25 22:09

Pierre L