Skip to content

Instantly share code, notes, and snippets.

@christophergandrud
Created October 13, 2014 14:23
Show Gist options
  • Save christophergandrud/33408807b2d0ab7b6bd0 to your computer and use it in GitHub Desktop.
Save christophergandrud/33408807b2d0ab7b6bd0 to your computer and use it in GitHub Desktop.
Convert text files in a directory to UTF-8 encoding (http://en.wikipedia.org/wiki/UTF-8)
#' Convert text files in a directory to UTF-8 encoding
#'
#' @param dir a character string naming the directory where the files are located.
#' @param new_dir a character string naming the directory where you would like the
#' converted files to be placed.
#'
#' @importFrom dplyr %>%
utf8_convert <- function(dir, new_dir){
all_files <- list.files(dir)
for (i in all_files){
old_path <- paste0('TextFiles_AllTogether/', i)
temp <- paste(readLines(old_path), collapse = ' ') %>%
iconv(to = 'UTF-8', sub = '')
cat(temp, file = paste0(new_dir, i))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment