Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- index value
- 1 1 1
- 2 2 1
- 3 3 1
- 4 4 1
- 5 5 1
- index value
- 1 4 3
- 2 5 3
- 3 6 3
- 4 7 3
- 5 8 3
- index value
- 1 1 1
- 2 2 1
- 3 3 1
- 4 4 2 (as (1+4)/2 = 2)
- 5 5 2 (as (1+4)/2 = 2)
- 6 6 3
- 7 7 3
- 8 8 3
- # locate the files
- files <- list.files(my.csv.dir)
- # read the files into a list of data.frames
- data.list <- lapply(files, read.csv)
- # concatenate into one big data.frame
- data.cat <- do.call(rbind, data.list)
- # aggregate
- data.agg <- aggregate(value ~ index, data.cat, mean)
- files <- list.files(my.csv.dir)
- algo.name <- sub("-.*", "", files)
- data.list <- lapply(files, read.csv)
- data.list <- Map(transform, data.list, algorithm = algo.name)
- data.cat <- do.call(rbind, data.list)
- data.agg <- aggregate(value ~ algorithm + index, data.cat, mean)
- ##Wanted to share below rMarkdown code that I gained from stackoverflow to solve my problem of combining multiple files into one master to analyze
- ```{r echo = FALSE, warning = FALSE, message = FALSE}
- Below is modified code of Daniel Marcelino
- ##set directory where you have your raw .csv files stored
- setwd("~/Data/R/Inventory/data/OnHandInventory/datacsv")
- ##below code will go into your directory and read all .csv files and rbind (stack) them into one master file as long as the headers are the same, if different look into smartbind()
- path = "~/Data/R/Inventory/data/OnHandInventory/datacsv/"
- out.file <- ""
- file.names <- dir(path, pattern =".csv")
- for(i in 1:length(file.names)){ file <- read.csv(file.names[i], header=TRUE, stringsAsFactors=FALSE)
- out.file <- rbind(out.file, file)
- }
- ##below writes out master file to specified directory
- write.csv(out.file, file="~/Data/R/Inventory/data/OnHandInventory/data/OnHandInv.csv", row.names = FALSE)
- ##go to new folder where combined file is located
- setwd("~/Data/R/Inventory/data/OnHandInventory/data")
- ##now read in the .csv
- OnHandInv <- read.csv("OnHandInv.csv", dec= ".", stringsAsFactors = FALSE)
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement