[英]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,使用base
和plyr
的替代答案是,
#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.