3

I am trying to parse out a file address and want to extract both the file location and the file name. For example, I want this:

"C:\Users\carriebrown\Desktop\test\Project_8754.csv"

to become this:

"C:\Users\carriebrown\Desktop\test\" and "Project_8754.csv"

I am attempting to use:

strsplit(file,'\\', fixed=TRUE)

But continue to the get the error:

Error in strsplit(file, "\", fixed = TRUE) : non-character argument

Is there a way to do this in R with strsplit? If not, is there a way to do it in a different manner?

Carrie Brown
  • 83
  • 1
  • 4

1 Answers1

2

How about this?

path <- unlist(strsplit('C:\\Users\\carriebrown\\Desktop\\test\\Project_8754.csv','\\',
                        fixed=TRUE))
file <- path[length(path)]
path <- paste(path[-length(path)], collapse='\\')
path
#[1] "C:\\Users\\carriebrown\\Desktop\\test"
file
#[1] "Project_8754.cs
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63