Guest User

Untitled

a guest
Nov 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. library(data.table)
  2. library(magrittr)
  3. library(stringr)
  4.  
  5. # create a function to delete unnecessary variables from a CAFAS or PECFAS
  6. data set and save the reduced copy
  7.  
  8. del.items <- function(file)
  9. {
  10. data <- read.csv(input = paste0("../data/pecfas|cafas/raw",
  11. str_match(pattern = "cafas|pecfas", string = file) %>% tolower, "/raw/",
  12. file), sep = ",", header = TRUE, na.strings = "", stringsAsFactors = FALSE,
  13. skip = 0, colClasses = "character", data.table = FALSE)
  14.  
  15. data <- data[-grep(pattern = "^(CA|PEC)FAS_E[0-9]+(T(Initial|[0-
  16. 9]+|Exit)|SP[a-z])_(G|S|Item)[0-9]+$", x = names(data))]
  17.  
  18. write.csv(data, file = paste0("../data/pecfas|cafas/items-del",
  19. str_match(pattern = "cafas|pecfas", string = file) %>% tolower, "/items-
  20. del/", sub(pattern = "ExportData_", x = file, replacement = "")) %>%
  21. tolower, sep = ",", row.names = FALSE, col.names = TRUE)
  22. }
  23.  
  24. # delete items from all cafas data sets
  25.  
  26. cafas.files <- list.files("../data/cafas/raw/", pattern = ".csv")
  27.  
  28. for (file in cafas.files){
  29. del.items(file)
  30. }
  31.  
  32. # delete items from all pecfas data sets
  33.  
  34. pecfas.files <- list.files("../data/pecfas/raw/", pattern = ".csv")
  35.  
  36. for (file in pecfas.files){
  37. del.items(file)
  38. }
Add Comment
Please, Sign In to add comment