I usually read a bunch of .csv files into a list of data frames and name it manually doing.
我通常会将一堆.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(...)
I want to do this renaming operation automatically. To do so, I created a data frame with the names I want for each of the data frames in my datos
list.
我想自动重命名操作。为此,我在我的数据列表中为每个数据框创建了一个数据框,其中包含我想要的名称。
Here is how I generate this data frame:
以下是我生成此数据框的方法:
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)
Because nombres_df$corrida[1]
is v1r1
, I have to name the datos$v1r1
columns ("estado","tiempo", nombres_df[1,2:5])
, and so on for the other 40 elements. I want to do this renaming automatically. I was thinking I could use something that uses regular expressions.
因为nombres_df $ corrida [1]是v1r1,所以我必须为其他40个元素命名数据$ v1r1列(“estado”,“tiempo”,nombres_df [1,2:5]),等等。我想自动重命名。我以为我可以使用使用正则表达式的东西。
Just for the record, I don't know why but the order of the list of data frames is not the same as the 1:20 sequence (by this I mean 10 comes before 2,3,4...)
仅仅为了记录,我不知道为什么,但数据帧列表的顺序与1:20序列不同(我的意思是10在2,3,4之前出现......)
Here's a toy example of a list with a similar structure but fewer and shorter data frames.
这是一个具有相似结构但数据帧越来越短的列表的玩具示例。
toy<-list(a=replicate(6,1:5),b=replicate(6,10:14))
1 个解决方案
#1
1
You have a data frame where variable corridas
is the name of the data frame to be renamed and the remaining columns are the desired variable names for that data frame. You could use a loop to do all the renaming operations:
您有一个数据框,其中变量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)])
}
#1
1
You have a data frame where variable corridas
is the name of the data frame to be renamed and the remaining columns are the desired variable names for that data frame. You could use a loop to do all the renaming operations:
您有一个数据框,其中变量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)])
}