0

So I have a bunch of different .csv files that I'd like to merge into a single data frame so I can run the code table at the end. What I'm trying to do is so it checks the name of the file [V(x)] so that if x < 7, then it merges [V(2:7)] together. I've found similar questions, but I can't really figure out how to put the condition for the [V(x)]. Here's what I have so far, and I'm not getting the right number of observations.

#Import data from all versions of survey
setwd("~09 Export")

#Change the following when new versions are added
allVer <- c(2, 3, 4, 5, 6, 7)
curVer <- 7
oldVer <- c(2, 3, 4, 5, 6)

#loop
dta <- data.frame()
for (i in allVer){
    dta <- rbind(dta, read.csv(paste("NH School Choice Survey [V", i, "]", ".csv", sep = "")), header = TRUE, stringsAsFactors = FALSE, fill = TRUE)
if (i < curVer){
    dta <- rbind(dta, read.csv(paste("NH School Choice Survey [V", i, "]", ".csv", sep = "")), header = TRUE, stringsAsFactors = FALSE, fill = TRUE)
    }
}
for (i in oldVer){
    dta <- rbind(dta, read.csv(paste("NH School Choice Survey [V", i, "]", ".csv", sep = "")), header = TRUE, stringsAsFactors = FALSE, fill = TRUE)
}

dta$completed <- dta$anyStudent == 1
table(dta$idSurveyor, dta$completed)
  • Possibly a combination of https://stackoverflow.com/a/41914882/3358272 with `list.files(pattern = "\\[V[2-7]\\]")`. – r2evans Jun 29 '17 at 05:56
  • See also [how to make a list of data frames](https://stackoverflow.com/a/24376207/903061). Something like `dta_list = lapply(list.files()); allver = data.table::rbindlist(dta_list)` should be a good start. – Gregor Thomas Jun 29 '17 at 06:55
  • So I'm trying `dta_list <- lapply(list.files(pattern = "NH School Choice Survey \\[V[2-7]\\].csv")) allver = data.table::rbindlist(dta_list) ` but it's returning `Error in match.fun(FUN) : argument "FUN" is missing, with no default` – Manuel Martinez Jun 30 '17 at 02:29
  • SUCCESS!!! `#Change \\[V[2-7]\\] depending on oldest-newest versions. nhlist <- list.files(pattern = "NH School Choice Survey \\[V[2-7]\\].csv") nhdat <- lapply(nhlist, read.csv) names(nhdat) <- stringr::str_replace(nhlist, pattern = ".csv", replacement = "") nhdat <- rbindlist(nhdat, fill = TRUE) nhdat$completed <- nhdat$anyStudent == 1 table(nhdat$idSurveyor, nhdat$completed)` – Manuel Martinez Jun 30 '17 at 02:55

0 Answers0