Importing Multiple .Csv Files into R and Adding a New Column with File Name

Importing multiple .csv files into R and adding a new column with file name

This should do it:

file_names <- dir("~/Desktop/data") 
df <- do.call(rbind, lapply(file_names, function(x) cbind(read.csv(x), name=strsplit(x,'\\.')[[1]][1])))

Read multiple csv into one and add a new column based on the file names

You can use sapply to read all the files in a list and with rbindlist combine them into one dataframe with a new column filename which has name of the file in every row.

library(data.table)
result <- rbindlist(sapply(files, fread,simplify = FALSE), idcol = 'filename')

How to add filename as a column to csv while reading & appending multiple csv's in r?

One option would be to use a named list of filenames. Afterwards you could add a column with the filename via the .id argument of map_df:

library(dplyr)
library(purrr)
library(readr)

mtcars_slim <- select(mtcars, 1:3)

write_csv(slice(mtcars_slim, 1:4), "AA_1.csv")
write_csv(slice(mtcars_slim, 5:10), "AAA_2.csv")
write_csv(slice(mtcars_slim, 11:1), "BBB_3.csv")

fn <- list.files(
path = ".",
pattern = "\\.csv",
full.names = T
)
names(fn) <- basename(fn)

map_df(fn, ~ read_csv(., show_col_types = FALSE), .id = "file")
#> # A tibble: 21 × 4
#> file mpg cyl disp
#> <chr> <dbl> <dbl> <dbl>
#> 1 AA_1.csv 21 6 160
#> 2 AA_1.csv 21 6 160
#> 3 AA_1.csv 22.8 4 108
#> 4 AA_1.csv 21.4 6 258
#> 5 AAA_2.csv 18.7 8 360
#> 6 AAA_2.csv 18.1 6 225
#> 7 AAA_2.csv 14.3 8 360
#> 8 AAA_2.csv 24.4 4 147.
#> 9 AAA_2.csv 22.8 4 141.
#> 10 AAA_2.csv 19.2 6 168.
#> # … with 11 more rows

Read in CSV files and Add a Column with File name

Here's a (mostly) tidyverse alternative that avoids looping:

library(tidyverse)

csv_names <- list.files(path = "path/", # set the path to your folder with csv files
pattern = "*.csv", # select all csv files in the folder
full.names = T) # output full file names (with path)
# csv_names <- c("file_1_october.csv", "file_2_november.csv")

csv_names2 <- data.frame(month = csv_names,
id = as.character(1:length(csv_names))) # id for joining

data <- csv_names %>%
lapply(read_csv) %>% # read all the files at once
bind_rows(.id = "id") %>% # bind all tables into one object, and give id for each
left_join(csv_names2) # join month column created earlier

This gives a single data object with data from all the CSVs together. In case you need them separately, you can omit the bind_rows() step, giving you a list of multiple tables ("tibbles"). These can then be split using list2env() or some split() function.

How to combine multiple .csv files, and add a column with each dataset's name, in R?

Here is a library(tidyverse) way of accomplishing what you need, you can still set your working directory to where it needs to be and instead of using dir() you can use list.files()

dat_files <- list.files(".../Historical Data", pattern='*.csv')

map_df(dat_files, ~read_csv(.x) %>%
mutate(month_year = str_remove_all(.x, ".csv", "")) %>%
separate(month_year, into=c("Month", "Year"), sep=" ")
)

This code will read all your files into one df and use the file name to create a new column without .csv attached to it. It will then separate the that column into the Month and Year column be separating on the " "

Add filename column to table as multiple files are read and bound

I generally use the following approach, based on dplyr/tidyr:

data = tibble(File = files) %>%
extract(File, "Site", "([A-Z]{2}-[A-Za-z0-9]{3})", remove = FALSE) %>%
mutate(Data = lapply(File, read_csv)) %>%
unnest(Data) %>%
select(-File)

Read csv files (more than 1) in r shiny and create new column for Filename

You can use input$datafile$datapath to read the file and input$datafile$name to add a new column with the file name. Use map2_df to pass both the values together and combine into one dataset.

library(shiny)
library(tidyverse)
library(data.table)

ui <- shinyUI(
fluidPage(
titlePanel("Example Read and Merge with new Column for Filename"),
sidebarLayout(
sidebarPanel( fileInput("datafile", h5("Choose CSV file:"),
accept = ".csv",multiple = TRUE)),
mainPanel(DT::dataTableOutput("Raw_data_show"),
verbatimTextOutput("results"),textOutput("filechosen"))
)))

server <- function(session,input, output) {

path <- reactiveValues(pth=NULL)

observeEvent(input$filechoose,{
path$pth <- file.choose()
})

output$filechosen <- renderText({

if(is.null(path$pth)){
return()
}else{
dirname(path$pth)
}
})

rawData <- reactiveValues(site = NULL)

observeEvent(input$datafile, {
req(input$datafile)
rawData$site <- map2_df(input$datafile$name, input$datafile$datapath,
~fread(.y)%>% mutate(FileName_2D = .x))


})

output$Raw_data_show <- DT::renderDataTable({
rawData$site
})

}

shinyApp(ui, server)


Related Topics



Leave a reply



Submit