-
Notifications
You must be signed in to change notification settings - Fork 0
/
concatanate_csvs.R
55 lines (43 loc) · 1.36 KB
/
concatanate_csvs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
library(stringr)
library(readr)
library(dplyr)
#folder path of your files
folder <- "/Users/BobaFett/Desktop/STAT_605/final_project/test/"
#set's the working directory to this path (for no reason really)
setwd(folder)
#list of files in the folder
file_list <- list.files(folder)
#list of only the .csv files
#this can be changes to .xlxs,
#but I'm not sure if R can read them
csv_list <- file_list[str_detect(file_list, fixed(".csv"))]
for (csv in csv_list){
#extracts a 5 digit number from the file name
#assuming that it's a zipcode
zipcode <- str_extract(csv, "([0-9]{5})")
# if the merged dataset doesn't exist, create it
if (!exists("dataset")){
#this line might need tweeking
#especially if not a csv
dataset <- read_delim(csv, col_names = TRUE, delim=",")
dataset$zipcode = zipcode
}
# otherwise if the merged dataset does exist, append to it
else if (exists("dataset")){
#this line too
temp_dataset <- read_delim(csv, col_names = TRUE, delim=",")
temp_dataset$zipcode = zipcode
#rowbind them
dataset <- rbind(dataset, temp_dataset)
#remove it for the next turn
#not really nescessary
rm(temp_dataset)
}
print(dataset)
}
#write the merged csv's up one folder
write_csv(dataset, path = "../merged_data.csv")
#run this if you want dataset
#to be empty again
#otherwise it will keep appending
#rm(dataset)