简体   繁体   English

Dataframe 使用 purrr 为数据帧列表中的列命名

[英]Dataframe name to column in list of dataframes using purrr

I have a list of data frames that are imported from excel files.我有一个从 excel 个文件导入的数据框列表。 Each file is imported and named after the batch they represent.每个文件都被导入并以它们代表的批次命名。

Below is an example:下面是一个例子:

library(tidyverse)
batch_1 <- data.frame(A = 1:3,
                      B = 4:6)
batch_2 <- data.frame(A = 1:3,
                      B = 4:6)
batch_3 <- data.frame(A = 1:3,
                      B = 4:6)
my_list <- list(batch_1, batch_2, batch_3)

I now want to create a new column in each of the data frames that is the name of each data frame.我现在想在每个数据框中创建一个新列,即每个数据框的名称。

So it will for each data frame look something like:所以它会为每个数据框看起来像:

  A B   batch
1 1 4 batch_1
2 2 5 batch_1
3 3 6 batch_1

which I will then combine to one data frame in order to plot. I can do it manually by mutate(batch = deparse(substitute(batch_1))) but I'm struggeling with "purrr-ifying" this.然后我会将其合并到一个数据框,以便 plot。我可以通过mutate(batch = deparse(substitute(batch_1)))手动完成,但我正在努力“purrr-ifying”这个。

map(my_list, ~mutate(batch = deparse(substitute(.x))))

gives an error: Error in UseMethod("mutate"): no applicable method for 'mutate' applied to an object of class "character"给出错误:UseMethod("mutate") 中的错误:没有适用于“mutate”的方法应用于 class“字符”的 object

It does not have to be purrr specific, any method is welcomed.它不必特定于 purrr,欢迎使用任何方法。

EDIT: @user63230 solution works.编辑:@user63230 解决方案有效。 But, as is typical, you find a solution when you already have one!但是,通常情况下,您会在已有解决方案时找到解决方案!

An alternative solution for this case is found in the later combination of data frames into one.这种情况的另一种解决方案是在后面将数据帧组合成一个。

bind_rows(my_list, .id = "batch") will added, an id column with the name of the data frame. bind_rows(my_list, .id = "batch")将添加一个带有数据框名称的 id 列。

Another way is to use lst instead of list which automatically names the list for you with imap which uses these names directly ( .y ).另一种方法是使用lst而不是list ,它会使用直接使用这些名称 ( .y ) 的imap自动为您命名列表。

library(tidyverse)
my_list <- lst(batch_1, batch_2, batch_3)
purrr::imap(my_list, ~mutate(.x, batch = .y))

# $batch_1
#   A B   batch
# 1 1 4 batch_1
# 2 2 5 batch_1
# 3 3 6 batch_1

# $batch_2
#   A B   batch
# 1 1 4 batch_2
# 2 2 5 batch_2
# 3 3 6 batch_2

# $batch_3
#   A B   batch
# 1 1 4 batch_3
# 2 2 5 batch_3
# 3 3 6 batch_3

Alternate answer using base and plyr would be,使用baseplyr的替代答案是,

#import all batch dataframes 
df= mget(grep(pattern = "bat", x = ls(), value = TRUE))

#convert the list to dataframe
df = ldply(df, as.data.frame)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用purrr :: walk重命名数据帧列表中的列 - rename a column in a list of dataframes in using purrr::walk 使用 purrr 将每月命名的数据框列表组合成每年的数据框列表 - Combine list of monthly named list of dataframes into one yearly name list of dataframes using purrr 如何在数据框列表中使用purrr :: map来修改特定数据框中的列值,而不更改列表中的其他数据框? - How do I use purrr::map with dataframe list to modify column values in specific dataframes without changing other dataframes in list? 使用 purrr 创建数据框列表以选择列 - create a list of dataframes using purrr to select columns purrr:在列表列中使用%in% - purrr: using %in% with a list-column 从数据帧列表中获取列名和数据帧名称到单个数据帧中 - Get column names and dataframe name from a list of dataframes into a single dataframe R:从数据帧列表中的 dataframe 名称设置列名称 - R: Set column name from dataframe name in list of dataframes 使用purrr :: map从自定义函数中的列表数据框输入列参数 - entering column arguments from list dataframes in a custom function using purrr::map 在列表的数据框中设置一列等于 R 列表中的 dataframe 的名称 - Set a column in dataframes of a list equal to the name of the dataframe in the list in R 使用 purrr 迭代替换数据框列中的字符串 - Using purrr to iteratively replace strings in a dataframe column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM