I have written a code that gets the required outcome. I want some help to shorten my code. What the code does:
- Retrieves path of all directories (and subdirectories) that has files in them
- Splits the lines in two columns - a) one column is the path and b)the other column is the file name with extension
I’m sure there can be a shorter version. Looking forward to the help.
Here is my code:
    library(stringr)
    setwd("/Users/Guest/Desktop/Project") #set Working Directory
    path <-"/Users/Guest/Desktop/Project"  #set path to retrieve files
    a <- list.files(path,recursive = TRUE) #retrieve files in variable a
    last <- str_locate(a,"(.*)/") #locate the last "/"
    sub <- str_sub(a,last[,2:2] + 1) #split from the last "/"
    adf <- as.data.frame(a,stringsAsFactors= FALSE) #convert to DF
    colnames(adf) <- "FPath" #ColumnName
    subdf <- as.data.frame(sub, stringsAsFactors = FALSE) #Convert to DF
    colnames(subdf) <- "FileName" #ColumnName
    Final <- cbind(adf,subdf) #Join both DF's
    Final <- within(Final, FileName <- ifelse(is.na(FileName), FPath, FileName)) #If there are files directly in root folder (Project), then FileName is NULL so replace it with FPath.
    Final
    write.table(Final, file = "Final_Import2.txt", quote = FALSE, row.names = FALSE, sep ="\t") #WritetoFile
 
    