来自数据框的数据框的名称列表

Name list of data frames from data frame

我通常将一堆 .csv 文件读入数据框列表并手动命名。

#...code for creating the list named "datos" with files from library 
# Naming the columns of the data frames
names(datos$v1r1)<-c("estado","tiempo","x1","x2","y1","y2")
names(datos$v1r2)<-c(...)
names(datos$v1r3)<-c(...)

我想自动执行此重命名操作。为此,我创建了一个数据框,其中包含 datos 列表中每个数据框所需的名称。

以下是我生成此数据框的方式:

pru<-rbind(c("UT","TR","UT+","TR+"),
            c("UT","TR","UT+","TR+"),
            c("TR","UT","TR+","UT+"),
            c("TR","UT","TR+","UT+"))

vec<-paste("v1r",seq(1,20,1),sep="")
tor<-paste("v1s",seq(1,20,1),sep="")


nombres<-do.call("rbind", replicate(10, pru, simplify = FALSE))

nombres_df<-data.frame(corrida=c(vec,tor),nombres)

因为 nombres_df$corrida[1]v1r1,我必须将 datos$v1r1 列命名为 ("estado","tiempo", nombres_df[1,2:5]),其他 40 个元素依此类推。 我想自动重命名。我在想我可以使用一些使用正则表达式的东西。

仅作记录,我不知道为什么数据帧列表的顺序与 1:20 序列不同(我的意思是 10 在 2、3、4 之前...)

这是一个列表的玩具示例,具有相似的结构,但数据帧更少更短。

toy<-list(a=replicate(6,1:5),b=replicate(6,10:14))

您有一个数据框,其中变量 corridas 是要重命名的数据框的名称,其余列是该数据框所需的变量名称。您可以使用循环来完成所有重命名操作:

for (i in seq_len(nrow(nombres_df))) {
  names(datos[[nombres_df$corridas[i]]]) <- c("estado","tiempo",nombres_df[i,2:length(nombres_df)])
}