Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Column Names in a List of Data Frames in R

Tags:

dataframe

r

Objective: Change the Column Names of all the Data Frames in the Global Environment from the following list

colnames of the ones in global environment

So.

0) The Column names are:

 colnames = c("USAF","WBAN","YR--MODAHRMN") 

1) I have the following data.frames: df1, df2.

2) I put them in a list:

  dfList <- list(df1,df2)

3) Loop through the list:

 for (df in dfList){
   colnames(df)=colnames
 }

But this creates a new df with the column names that I need, it doesn't change the original column names in df1, df2. Why? Could lapply be a solution? Thanks

Can something like:

 lapply(dfList, function(x) {colnames(dfList)=colnames})

work?

like image 986
Oniropolo Avatar asked Feb 21 '15 17:02

Oniropolo


People also ask

How do I change column names in a list of Dataframes in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

How do I change multiple column names in a Dataframe in R?

rename() is the method available in the dplyr library which is used to change the multiple columns (column names) by name in the dataframe. The operator – %>% is used to load the renamed column names to the dataframe. At a time it will change single or multiple column names.

Can we rename the column values of data frame?

The columns can also be renamed by directly assigning a list containing the new names to the columns attribute of the Dataframe object for which we want to rename the columns.


Video Answer


3 Answers

With lapply you can do it as follows.

Create sample data:

df1 <- data.frame(A = 1, B = 2, C = 3)
df2 <- data.frame(X = 1, Y = 2, Z = 3)
dfList <- list(df1,df2)
colnames <- c("USAF","WBAN","YR--MODAHRMN") 

Then, lapply over the list using setNames and supply the vector of new column names as second argument to setNames:

lapply(dfList, setNames, colnames)
#[[1]]
#  USAF WBAN YR--MODAHRMN
#1    1    2            3
#
#[[2]]
#  USAF WBAN YR--MODAHRMN
#1    1    2            3

Edit

If you want to assign the data.frames back to the global environment, you can modify the code like this:

dfList <- list(df1 = df1, df2 = df2)
list2env(lapply(dfList, setNames, colnames), .GlobalEnv)
like image 70
talat Avatar answered Oct 08 '22 18:10

talat


Just change your for-loop into an index for-loop like this:

Data

df1 <- data.frame(a=runif(5), b=runif(5), c=runif(5))
df2 <- data.frame(a=runif(5), b=runif(5), c=runif(5))

dflist <- list(df1,df2)

colnames = c("USAF","WBAN","YR--MODAHRMN") 

Solution

for (i in seq_along(dflist)){
  colnames(dflist[[i]]) <- colnames
}

Output

> dflist
[[1]]
       USAF      WBAN YR--MODAHRMN
1 0.8794153 0.7025747    0.2136040
2 0.8805788 0.8253530    0.5467952
3 0.1719539 0.5303908    0.5965716
4 0.9682567 0.5137464    0.4038919
5 0.3172674 0.1403439    0.1539121

[[2]]
        USAF       WBAN YR--MODAHRMN
1 0.20558383 0.62651334    0.4365940
2 0.43330717 0.85807280    0.2509677
3 0.32614750 0.70782919    0.6319263
4 0.02957656 0.46523151    0.2087086
5 0.58757198 0.09633181    0.6941896

By using for (df in dfList) you are essentially creating a new df each time and change the column names to that leaving the original list (dfList) untouched.

like image 25
LyzandeR Avatar answered Oct 08 '22 18:10

LyzandeR


If you want the for loop to work, you should not pass the whole data.frame as the argument.

for (df in 1:length(dfList))
  colnames(dfList[[df]]) <- colnames
like image 1
StrikeR Avatar answered Oct 08 '22 18:10

StrikeR