[英]Rename a data frame's variables stored in a list using the object name
I could not solve a problem despite having studied the related entries. 尽管研究了相关条目,但我无法解决问题。 I have a list of many dataframes, each with two columns (variables).
我有许多数据框的列表,每个数据框都有两列(变量)。 I want to rename the second column of each dataframe assigning the name of the dataframe to which it belongs (not including the extension ".csv").
我想重命名每个数据框的第二列,为其分配所属数据框的名称(不包括扩展名“ .csv”)。 Specifically I need to replace a variable's name with the object's name where the variable is in.
具体来说,我需要用变量所在的对象的名称替换变量的名称。
So, for example, I have a list of 3 dataframes 因此,例如,我有一个3个数据框的列表
List of 3
$ Conc.csv1: Classes 'data.table' and 'data.frame': 83 obs of 2 variables.
.. $ Type: chr [1:83] "FAMILY" ...
.. $ Process: int [1:83] 2304 ... NA NA
$ Ambs.csv2: Classes 'data.table' and 'data.frame': 78 obs. of 2 variables:
.. $ Type: chr [1:78] "CIVIL AND COMMERCIAL" ...
.. $ Process: int [1:78] NA 2 ...
$ Sec.csv3: Classes 'data.table' and 'data.frame': 117 obs. of 2 variables:
.. $ Type: chr [1: 117] "Action"
.. $ Process: int [1: 117] NA NA 110 ...
I need the same list but renaming the variable "Process" in every dataframe with the name of the dataframe 我需要相同的列表,但在每个数据框中将变量“ Process”重命名为数据框的名称
So, Output should look like: 因此,输出应如下所示:
List of 3
$ Conc.csv1: Classes 'data.table' and 'data.frame': 83 obs of 2 variables.
.. $ Type: chr [1:83] "FAMILY" ...
.. $ Conc: int [1:83] 2304 ... NA NA
$ Ambs.csv2: Classes 'data.table' and 'data.frame': 78 obs of 2 variables.
.. $ Type: chr [1:78] "CIVIL AND COMMERCIAL" ...
.. $ Ambs: int [1:78] NA 2 ...
$ Sec.csv3: Classes 'data.table' and 'data.frame' 117 obs of 2 variables.
.. $ Type: chr [1: 117] "Action"
.. $ Sec: int [1: 117] NA NA 110 ...
There are certainly many ways to do this, but I chose the good old for
loop: 当然有很多方法可以做到这一点,但是我选择了很好的
for
循环:
l <- list(Conc.csv1 = data.frame(Type = NA, Process = NA),
Ambs.csv1 = data.frame(Type = NA, Process = NA),
Sec.csv1 = data.frame(Type = NA, Process = NA))
for (df in seq_along(l)) {
colnames(l[[df]])[2] <- gsub("\\.csv\\d", "", names(l)[df])
}
str(l)
#List of 3
# $ Conc.csv1:'data.frame': 1 obs. of 2 variables:
# ..$ Type: logi NA
# ..$ Conc: logi NA
# $ Ambs.csv1:'data.frame': 1 obs. of 2 variables:
# ..$ Type: logi NA
# ..$ Ambs: logi NA
# $ Sec.csv1 :'data.frame': 1 obs. of 2 variables:
# ..$ Type: logi NA
# ..$ Sec : logi NA
> library("purrr")
> l <- list(Conc.csv1 = data.frame(Type = 1:5, Process = 1:5),
Ambs.csv1 = data.frame(Type = 6:10, Process = 1:5),
Sec.csv1 = data.frame(Type = 11:15, Process = 1:5))
> str(l)
List of 3
$ Conc.csv1:'data.frame': 5 obs. of 2 variables:
..$ Type : int [1:5] 1 2 3 4 5
..$ Process: int [1:5] 1 2 3 4 5
$ Ambs.csv1:'data.frame': 5 obs. of 2 variables:
..$ Type : int [1:5] 6 7 8 9 10
..$ Process: int [1:5] 1 2 3 4 5
$ Sec.csv1 :'data.frame': 5 obs. of 2 variables:
..$ Type : int [1:5] 11 12 13 14 15
..$ Process: int [1:5] 1 2 3 4 5
> l_names <- as.list(gsub( "\\.csv1$", "",names(l)))
> l_df <- map2(.x = l, .y = l_names, .f = ~ {colnames(.x)[2] <- .y; return(.x)})
> str(l_df)
List of 3
$ Conc.csv1:'data.frame': 5 obs. of 2 variables:
..$ Type: int [1:5] 1 2 3 4 5
..$ Conc: int [1:5] 1 2 3 4 5
$ Ambs.csv1:'data.frame': 5 obs. of 2 variables:
..$ Type: int [1:5] 6 7 8 9 10
..$ Ambs: int [1:5] 1 2 3 4 5
$ Sec.csv1 :'data.frame': 5 obs. of 2 variables:
..$ Type: int [1:5] 11 12 13 14 15
..$ Sec : int [1:5] 1 2 3 4 5
You could also use lapply
as follows. 您还可以如下使用
lapply
。 Assuming you have a list named l as in the other examples: 假设您有一个名为l的列表,如其他示例所示:
newNames <- gsub("\\..*", "", names(l))
l <- lapply(seq_along(newNames),
setNames(l[[i]], c(names(l[[i]])[1], newNames[i])))
This will return a list with the data.frames renamed. 这将返回一个已重命名data.frames的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.