0

I have written a code that gets the required outcome. I want some help to shorten my code. What the code does:

  1. Retrieves path of all directories (and subdirectories) that has files in them
  2. 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
CuriousBeing
  • 1,592
  • 14
  • 34

1 Answers1

1

Here is one approach that may be of help aside from the provided answers in the link:

library(gsubfn)
m <- strapply(a, '(.*)/(.*)', ~ c(FPath=x, FileName=y), simplify=rbind)
Final <- as.data.frame(m, stringsAsFactors = FALSE)
hwnd
  • 69,796
  • 4
  • 95
  • 132