0

I have a list of data frame with varying column count and names (i.e, A). I want to change the column name for each data frame in the list based on the another data frame(i.e., B)

The B data data frame has 2 fields. 1) Colname and 2) Modified column name

I want to compare the colname from the B data frame with each date frame in the list A and rename with the modified column name.

Based on the some previous answers, I tried to change the column names like below and not able to get the desired output.

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

Further, I want to loop this for all the data frames in the list to rename the column name based on the B data frame.

Updated:

The list of data frame (A) looks like below

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")))

And the data frame (B) with modified names looks like below:

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"))

And the desired output will be:

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"))
ssan
  • 301
  • 1
  • 9
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 22 '19 at 15:17

1 Answers1

2

You could try something like this for individual list elements, which is based on your attempt:

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

To change the names of every list element you can put the above into lapply (I use names<- to avoid having to use return):

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

Once your data frames all have the same column names you can use do.call with rbind to combine them:

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    

You could also try something like this, which is more succinct, albeit also harder to understand:

library(tidyverse)

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