If I understand correctly, you want to add column names to a list of data.frames. Your code is confusing because you remove the data frames and then use their names for the column names:
sec1 <- data.frame(x1 = 1:5,
x2 =c("L","M","N","O","P"),
x3 = c(4, 1, 9, 0, 1))
sec2 <- data.frame(x1 = c("A","B","C","D","E"),
x2 = 1:5,
x3 = c(12, 10, 29, 0, 5))
sec3 <- data.frame(x1 = c(100, 21, 91, 53, 35),
x2 = 1:5,
x3 = c("W","X","Y","Z","A"))
listDTA <- list(sec1=sec1, sec2=sec2, sec3=sec3)
listAA <- list(sec1 = c("A","B","C"),
sec2 = c("AA","BB","CC"),
sec3 = c("AAA","BBB","CCC"))
Now use a loop to assign the column names in listDTA
to the data frames in list listAA
:
n <- length(listDTA)
for (i in seq_len(n)) {
colnames(listDTA[[i]]) <- listAA[[i]]
}
listDTA
# $sec1
# A B C
# 1 1 L 4
# 2 2 M 1
# 3 3 N 9
# 4 4 O 0
# 5 5 P 1
# $sec2
# AA BB CC
# 1 A 1 12
# 2 B 2 10
# 3 C 3 29
# 4 D 4 0
# 5 E 5 5
# $sec3
# AAA BBB CCC
# 1 100 1 W
# 2 21 2 X
# 3 91 3 Y
# 4 53 4 Z
# 5 35 5 A
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…