Home > Enterprise >  dplyr get access to dot operator within mutate
dplyr get access to dot operator within mutate

Time:05-09

I'm trying to read in every file from a directory, but prior to binding the data.frames to each other with map_df, I want to create a column that stores the file name of data.frame.

Here are failed various attempts:

data <- 
    list.files(path, pattern = "*.csv") %>% 
    map_df(~fread(paste0(path, .)), ~mutate(file_name = .)) 

data <- 
    list.files(path, pattern = "*.csv") %>% 
    map_df(~fread(paste0(path, .))) %>% mutate(file_name = .)

data <- 
    list.files(path, pattern = "*.csv") %>% 
    map_df(~fread(paste0(path, .))) %>% mutate(~file_name = .)

However, I can't seem to figure out how to access the dot operator in the mutate function to get the file name. Any ideas?

CodePudding user response:

map_dfr() is designed for this. It has an arg .id to create a new column storing either the name (if .x is named) or the index (if .x is unnamed) of the input.

data <- list.files(path, pattern = "*.csv") %>%
  set_names() %>%
  map_dfr(~ fread(paste0(path, .x)), .id = "file_name") 

set_names() is used to set names of a vector, and default to name an element as itself. E.g.

c("file_1.csv", "file_1.csv") %>% set_names()

#   file_1.csv   file_1.csv  (name)
# "file_1.csv" "file_1.csv"  (value)
  • Related