更新时间:2023-02-18 09:14:58
Below is a code that renames the column names of each data.frame in a list in a way so that names of data.frames are added to original column names.
# example data
a <- data.frame(col1 = 1:10, col2 = 10:1)
b <- data.frame(col_01 = 11:20, col_02 = 20:11)
# list of data.frames
list_of_df <- list(a, b)
# names of data.frames
names(list_of_df) <- c("a", "b")
# my sequence and names of data.frames in a list
my_seq <- seq_along(list_of_df)
my_list_names <- names(list_of_df)
# procedure
for (i in my_seq) {
names(list_of_df[[my_seq[i]]]) <-
paste(my_list_names[i], names(list_of_df[[my_seq[i]]]), sep = "_")
}
list_of_df
$a
a_col1 a_col2
1 1 10
2 2 9
3 3 8
4 4 7
5 5 6
6 6 5
7 7 4
8 8 3
9 9 2
10 10 1
$b
b_col_01 b_col_02
1 11 20
2 12 19
3 13 18
4 14 17
5 15 16
6 16 15
7 17 14
8 18 13
9 19 12
10 20 11