A collection of code snippets that someday seemed useful.

read_complex_csv.R 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # load packages -----------------------------------------------------------
  2. require("tidyverse")
  3. # define header identification --------------------------------------------
  4. header_id <- "ID"
  5. # read raw data -----------------------------------------------------------
  6. RAW <- readLines(file.choose())
  7. head(RAW)
  8. # find lines to split at --------------------------------------------------
  9. header_lines <- which(substr(RAW, 1, nchar(header_id)) == header_id)
  10. # create a list of all datasets within the CSV file -----------------------
  11. DATASETS <- tibble(
  12. Row = 1:length(RAW),
  13. Data = RAW,
  14. Split = ifelse(Row %in% header_lines, header_lines, NA)
  15. ) %>%
  16. fill(Split) %>%
  17. split(f = .$Split)
  18. # extract relevant data ---------------------------------------------------
  19. BURGERS <- tibble(
  20. ID = character(),
  21. Bun = numeric(),
  22. Patty = numeric(),
  23. Salat = numeric(),
  24. Tomate = numeric()
  25. )
  26. for(i in 1:length(DATASETS)){
  27. # print loop ............................................................
  28. print(i)
  29. # extract data ..........................................................
  30. tmp_str <- DATASETS[[i]] %>% select(Data) %>% unlist()
  31. tmp_data <- read_delim(paste(tmp_str, collapse="\n"), delim = ";", na = "<LOD") %>%
  32. select(
  33. ID
  34. matches(names(BURGERS))
  35. ) %>%
  36. filter(!is.na(ID))
  37. # append to results .....................................................
  38. BURGERS <- BURGERS %>% bind_rows(tmp_data)
  39. }