简体   繁体   English

使用lapply更改数据框列表的列名

[英]Using lapply to change column names of a list of data frames

I'm trying to use lapply on a list of data frames; 我试图在数据框列表上使用lapply ; but failing at passing the parameters correctly (I think). 但没有正确传递参数(我认为)。

List of data frames: 数据框列表:

df1 <- data.frame(A = 1:10, B= 11:20)
df2 <- data.frame(A = 21:30, B = 31:40) 

listDF <- list(df1, df2,df3)    #multiple data frames w. way less columns than the length of vector todos

Vector with columns names: 矢量与列名称:

todos <-c('col1','col2', ......'colN')

I'd like to change the column names using lapply: 我想使用lapply更改列名:

lapply (listDF, function(x) { colnames(x)[2:length(x)] <-todos[1:length(x)-1] }  )

but this doesn't change the names at all. 但这根本不会改变名字。 Am I not passing the data frames themselves, but something else? 我不是自己传递数据帧,而是其他什么? I just want to change names, not to return the result to a new object. 我只想更改名称,而不是将结果返回给新对象。

Thanks in advance, p. 在此先感谢,p。

You can also use setNames if you want to replace all columns 如果要替换所有列,也可以使用setNames

df1 <- data.frame(A = 1:10, B= 11:20)
df2 <- data.frame(A = 21:30, B = 31:40) 

listDF <- list(df1, df2)
new_col_name <- c("C", "D")

lapply(listDF, setNames, nm = new_col_name)
## [[1]]
##     C  D
## 1   1 11
## 2   2 12
## 3   3 13
## 4   4 14
## 5   5 15
## 6   6 16
## 7   7 17
## 8   8 18
## 9   9 19
## 10 10 20

## [[2]]
##     C  D
## 1  21 31
## 2  22 32
## 3  23 33
## 4  24 34
## 5  25 35
## 6  26 36
## 7  27 37
## 8  28 38
## 9  29 39
## 10 30 40

If you need to replace only a subset of column names, then you can use the solution of @Jogo 如果您只需要替换列名称的子集,则可以使用@Jogo的解决方案

lapply(listDF, function(df) {
  names(df)[-1] <- new_col_name[-ncol(df)]
  df
})

A last point, in R there is a difference between a:b - 1 and a:(b - 1) 最后一点,在R中,a:b - 1和a:(b - 1)之间存在差异

1:10 - 1
## [1] 0 1 2 3 4 5 6 7 8 9

1:(10 - 1)
## [1] 1 2 3 4 5 6 7 8 9

EDIT 编辑

If you want to change the column names of the data.frame in global environment from a list, you can use list2env but I'm not sure it is the best way to achieve want you want. 如果要从列表中更改全局环境中data.frame的列名,可以使用list2env但我不确定这是实现您想要的最佳方式。 You also need to modify your list and use named list, the name should be the same as name of the data.frame you need to replace. 您还需要修改列表并使用命名列表,该名称应与您需要替换的data.frame名称相同。

listDF <- list(df1 = df1, df2 = df2)

new_col_name <- c("C", "D")

listDF <- lapply(listDF, function(df) {
  names(df)[-1] <- new_col_name[-ncol(df)]
  df
})

list2env(listDF, envir = .GlobalEnv)
str(df1)
## 'data.frame':    10 obs. of  2 variables:
##  $ A: int  1 2 3 4 5 6 7 8 9 10
##  $ C: int  11 12 13 14 15 16 17 18 19 20

try this: 尝试这个:

lapply (listDF, function(x) { 
  names(x)[-1] <- todos[-length(x)]
  x 
})

you will get a new list with changed dataframes. 您将获得一个包含已更改数据框的新列表。 If you want to manipulate the listDF directly: 如果要直接操作listDF

for (i in 1:length(listDF)) names(listDF[[i]])[-1] <- todos[-length(listDF[[i]])]

I was not able to get the code used in these answers to work. 我无法将这些答案中使用的代码用于工作。 I found some code from another forum which did work. 我从其他论坛找到了一些有用的代码。 This will assign the new column names into each dataframe, the other methods created a copy of the dataframes. 这会将新列名分配到每个数据框中,其他方法会创建数据框的副本。 For anyone else here is the code. 这里的任何人都是代码。

# Create some dataframes
df1 <- data.frame(A = 1:10, B= 11:20)
df2 <- data.frame(A = 21:30, B = 31:40)

listDF <- c("df1", "df2") #Notice this is NOT a list
new_col_name <- c("C", "D") #What do you want the new columns to be named?

# Assign the new column names to each dataframe in "listDF"
for(df in listDF) {
  df.tmp <- get(df)
  names(df.tmp) <- new_col_name
  assign(df, df.tmp)
}

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

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