Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

purrr

I have a list of data frames that are imported from excel files. 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.

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"

It does not have to be purrr specific, any method is welcomed.

EDIT: @user63230 solution works. 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.

like image 718
Godrim Avatar asked Oct 12 '25 18:10

Godrim


1 Answers

Another way is to use lst instead of list which automatically names the list for you with imap which uses these names directly (.y).

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!