根据其他数据框更改数据框列表的列名

时间:2019-10-22 15:15:51

标签: r datatable dplyr

我有一个具有不同列数和名称(即A)的数据框列表。我想根据另一个数据框(即B)更改列表中每个数据框的列名

B数据数据帧具有2个字段。 1)名称和2)修改后的列名

我想将B数据框中的列名与列表A中的每个日期框进行比较,并使用修改后的列名进行重命名。

基于先前的一些答案,我试图更改如下所示的列名,但无法获得所需的输出。

names(A[[1]]) <- B[match(names(A[[1]]),B[,"Modified column name"]),"Colname"] 

此外,我想循环使用列表中的所有数据框,以根据B数据框重命名列名。

已更新:

数据框(A)的列表如下所示

list(structure(list(i_id = c(1, 2, 3, 4, 5), i_reason = c("Event off", 
"Event on", "lock", "invalid", "valid"), i_name = c("A", "B", 
"C", "D", "E")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(id = c(6, 7, 8, 9, 10), 
    reasoncode = c("Event off", "Event on", "lock", "invalid", 
    "valid"), `first name` = c("A", "B", "C", "D", "E")), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    identifier = c(11, 12, 13, 14, 15), reasoncode = c("Event off", 
    "Event on", "lock", "invalid", "valid"), `sur name` = c("A", 
    "B", "C", "D", "E")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame")))

具有修改名称的数据框(B)如下所示:

structure(list(Colname = c("i_id", "i_reason", "i_name", "id", 
"reasoncode", "first name", "identifier", "reasoncode", "sur name"
), `Modified column name` = c("ID", "Reason", "Name", "ID", "Reason", 
"Name", "ID", "Reason", "Name")), row.names = c(NA, -9L), class = c("tbl_df", 
"tbl", "data.frame"))

所需的输出将是:

structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15), Reason = c("Event off", "Event on", "lock", "invalid", 
"valid", "Event off", "Event on", "lock", "invalid", "valid", 
"Event off", "Event on", "lock", "invalid", "valid"), Name = c("A", 
"B", "C", "D", "E", "A", "B", "C", "D", "E", "A", "B", "C", "D", 
"E")), row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:2)

您可以为单个列表元素尝试类似的操作,具体取决于您的尝试:

names(A[[1]]) <- B$`Modified column name`[match(names(A[[1]]), B$Colname)]

要更改每个列表元素的名称,可以将以上内容放入lapply中(我使用names<-以避免使用return):

clean_B <- lapply(A, function(df){
    `names<-`(df, B$`Modified column name`[match(names(df), B$Colname)])
})

一旦所有数据框都具有相同的列名,就可以将do.callrbind结合使用:

do.call(rbind, clean_B)

#### OUTPUT ####

# A tibble: 15 x 3
      ID Reason    Name 
   <dbl> <chr>     <chr>
 1     1 Event off A    
 2     2 Event on  B    
 3     3 lock      C    
 4     4 invalid   D    
 5     5 valid     E    
 6     6 Event off A    
 7     7 Event on  B    
 8     8 lock      C    
 9     9 invalid   D    
10    10 valid     E    
11    11 Event off A    
12    12 Event on  B    
13    13 lock      C    
14    14 invalid   D    
15    15 valid     E    

您也可以尝试这样的方法,虽然更难理解,但它更简洁:

library(tidyverse)

map_dfr(A,
    ~ rename(., !!! `names<-`(B[[1]], B[[2]])[match(names(.), B[[1]])])
    )