Read Files Function

This is a function that reads files from a directory and assigns them to the global environment. It gives their variables in the global environment the same name that the original files have in the directory.

read_files_function <- function(ListOfFilesWithFullNames){
        ldf <- lapply(ListOfFilesWithFullNames, read.table)
        
        cleanedfilenames <- vector()
        for (i in 1:length(ListOfFilesWithFullNames)){
                splitfilename <- strsplit(as.character(ListOfFilesWithFullNames[i]), "/")[[1]]
                cleanedfilenames[i] <- splitfilename[length(splitfilename)]        
        }
        assign(paste(strsplit(ListOfFilesWithFullNames, "/")[[1]][4],"_files", sep=""), cleanedfilenames)
        for (i in 1:length(ldf)){
                assign(paste(cleanedfilenames[i]), ldf[[i]], envir = .GlobalEnv)
        }   
}
ls()
## [1] "read_files_function" "url"
filenames1 <- list.files("./data/UCI HAR Dataset/test", pattern="*.txt", full.names=TRUE)
read_files_function(filenames1)
ls()
## [1] "filenames1"          "read_files_function" "subject_test.txt"   
## [4] "url"                 "X_test.txt"          "y_test.txt"

It’s important that the function is given full file names. For example:

filenames1
## [1] "./data/UCI HAR Dataset/test/subject_test.txt"
## [2] "./data/UCI HAR Dataset/test/X_test.txt"      
## [3] "./data/UCI HAR Dataset/test/y_test.txt"

The dataset I use in this example can be found here: http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones