简体   繁体   English

使用对象名称重命名存储在列表中的数据框变量

[英]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.

相关问题 使用数据框的名称(删除,替换和套用)重命名数据框列表中的第二列 - Rename the Second Column in a List of Data Frames using the Name of the Data Frame (deparse, substitute, and lapply) 使用列表中的匹配项或部分匹配项重命名数据框中的 row.name - Rename row.name in data frame using matches or partial matches from a list 重命名数据框中的变量子集 - Rename subset of variables in data frame 使用循环和 dplyr 重命名列表中数据框的列 - Rename the column of a data frame within a list using a loop and dplyr R:重命名数据框中的变量子集 - R: rename subset of variables in data frame 使用来自存储变量的符号名称重命名数据框列? - Renaming data frame columns using symbolic names from stored variables? 在 for 循环中使用带有字符列表的数据框列作为 function 的变量名称,将结果放入变量中 - Using data frame column with character list as name of variables for function in for loop putting results in variable 如何使用 object 在 for 循环中命名数据框? - How to name data frame in for loops using object? 如何使用现有变量重命名多维数据框中的变量(&gt; 3000 列)? - How to rename variables in multidimensional data frame (>3000 columns) using existing variables? 如何按数据框中的列名重命名观察结果? - How to rename observations by column name in data frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM