Created
February 12, 2018 21:02
-
-
Save randallhelms/57757917d2fd1eac70461f8b5cbb193c to your computer and use it in GitHub Desktop.
A way to read in multiple text files in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#load relevant packages | |
library(dplyr) | |
library(tidyr) | |
library(readr) | |
library(stringr) | |
library(lubridate) | |
library(downloader) | |
#get zip file and unzip it to your new subfolder | |
zip_url <- 'http://sonicrampage.org/mixes/carl_cox/carl_cox_sets.zip' | |
download(zip_url,dest="carl_cox_sets.zip",mode="wb") | |
unzip("carl_cox_sets.zip",exdir='carl_cox_sets') | |
#change to new directory | |
setwd('./carl_cox_sets') | |
readin <- function(df) { | |
cols <- c('artist','title') | |
x <- read_delim(df,delim=",",col_names = TRUE) %>% | |
#filter to just artist and title | |
select(artist,title) %>% | |
#create the party and year columns | |
mutate(party = df) %>% | |
mutate(party = str_replace_all(party,"_"," "), | |
party = str_replace(party,".txt","")) %>% | |
mutate(year = str_sub(party,-4)) %>% | |
mutate_all(funs(str_to_title)) %>% | |
#add the tracklisting numbers | |
group_by(party) %>% | |
mutate(order = row_number()) %>% | |
ungroup() %>% | |
#tidy up the data so that DJ is always written correctly | |
mutate_at(cols,funs(gsub("Dj", "DJ",.))) %>% | |
arrange(party,order,year) | |
return(x) | |
} | |
file_list <- list.files() | |
datalist <- lapply(file_list,readin) | |
coxy <- do.call("bind_rows",datalist) | |
#write the file to the parent directory | |
setwd("..") | |
write.table(coxy,file="carl_cox_sets.csv",sep=",",row.names = FALSE,col.names = TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment