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