简体   繁体   English

在 R 中更改数据框列表中的列名称

[英]Changing Column Names in a List of Data Frames in 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: 0)列名是:

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

1) I have the following data.frames: df1, df2. 1)我有以下数据帧:df1、df2。

2) I put them in a list: 2)我把它们放在一个列表中:

  dfList <- list(df1,df2)

3) Loop through the list: 3)遍历列表:

 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.但这会创建一个包含我需要的列名的新 df,它不会更改 df1、df2 中的原始列名。 Why?为什么? Could lapply be a solution? lapply 可以解决吗? Thanks谢谢

Can something like:可以这样:

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

work?工作?

With lapply you can do it as follows.使用 lapply 您可以按如下方式进行。

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 :然后,使用setNames对列表进行setNames并提供新列名称的向量作为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:如果要将 data.frames 分配回全局环境,可以像这样修改代码:

dfList <- list(df1 = df1, df2 = df2)
list2env(lapply(dfList, setNames, colnames), .GlobalEnv)

Just change your for-loop into an index for-loop like this:只需将您的 for 循环更改为这样的索引 for 循环:

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.通过使用for (df in dfList)您实际上每次都在创建一个新的 df 并将列名更改为保留原始列表( dfList )不变的列名。

If you want the for loop to work, you should not pass the whole data.frame as the argument.如果您希望for循环工作,则不应将整个 data.frame 作为参数传递。

for (df in 1:length(dfList))
  colnames(dfList[[df]]) <- colnames
dfList <- lapply(dfList, `names<-`, colnames)

Create the sample data:创建示例数据:

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

Then create a function to set the colnames:然后创建一个 function 来设置colnames:

res=lapply(dfList, function(x){colnames(x)=c(name);x})

[[1]]
USAF WBAN YR--MODAHRMN
1    1    2            3

[[2]]
USAF WBAN YR--MODAHRMN
1    1    2            3

A tidyverse solution with rename_with :使用rename_withtidyverse解决方案:

library(dplyr)
library(purrr)

map(dflist, ~ rename_with(., ~ colnames))

Or, if it's only for one column:或者,如果它仅用于一列:

map(dflist, ~ rename(., new_col = old_col))

This also works with lapply :这也适用于lapply

lapply(dflist, rename_with, ~ colnames)
lapply(dflist, rename, new_col = old_col)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM