Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. index value
  2. 1 1 1
  3. 2 2 1
  4. 3 3 1
  5. 4 4 1
  6. 5 5 1
  7.  
  8. index value
  9. 1 4 3
  10. 2 5 3
  11. 3 6 3
  12. 4 7 3
  13. 5 8 3
  14.  
  15. index value
  16. 1 1 1
  17. 2 2 1
  18. 3 3 1
  19. 4 4 2 (as (1+4)/2 = 2)
  20. 5 5 2 (as (1+4)/2 = 2)
  21. 6 6 3
  22. 7 7 3
  23. 8 8 3
  24.  
  25. # locate the files
  26. files <- list.files(my.csv.dir)
  27.  
  28. # read the files into a list of data.frames
  29. data.list <- lapply(files, read.csv)
  30.  
  31. # concatenate into one big data.frame
  32. data.cat <- do.call(rbind, data.list)
  33.  
  34. # aggregate
  35. data.agg <- aggregate(value ~ index, data.cat, mean)
  36.  
  37. files <- list.files(my.csv.dir)
  38. algo.name <- sub("-.*", "", files)
  39. data.list <- lapply(files, read.csv)
  40. data.list <- Map(transform, data.list, algorithm = algo.name)
  41. data.cat <- do.call(rbind, data.list)
  42. data.agg <- aggregate(value ~ algorithm + index, data.cat, mean)
  43.  
  44. ##Wanted to share below rMarkdown code that I gained from stackoverflow to solve my problem of combining multiple files into one master to analyze
  45. ```{r echo = FALSE, warning = FALSE, message = FALSE}
  46. Below is modified code of Daniel Marcelino
  47. ##set directory where you have your raw .csv files stored
  48. setwd("~/Data/R/Inventory/data/OnHandInventory/datacsv")
  49. ##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()
  50.  
  51. path = "~/Data/R/Inventory/data/OnHandInventory/datacsv/"
  52. out.file <- ""
  53. file.names <- dir(path, pattern =".csv")
  54. for(i in 1:length(file.names)){ file <- read.csv(file.names[i], header=TRUE, stringsAsFactors=FALSE)
  55. out.file <- rbind(out.file, file)
  56. }
  57. ##below writes out master file to specified directory
  58. write.csv(out.file, file="~/Data/R/Inventory/data/OnHandInventory/data/OnHandInv.csv", row.names = FALSE)
  59. ##go to new folder where combined file is located
  60. setwd("~/Data/R/Inventory/data/OnHandInventory/data")
  61. ##now read in the .csv
  62. OnHandInv <- read.csv("OnHandInv.csv", dec= ".", stringsAsFactors = FALSE)
  63. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement