Andrei Niță
Andrei Niță

Reputation: 619

How do I extract the path before the "/" in R for multiple list.files?

Let's say I have the next vector obtained by using list.files() in R:

paths <- c("/home/user/myfolder/ERA/amsterdam/amsterdam1.nc", "/home/user/myfolder/ERA/amsterdam/amsterdam2.nc", 
       "/home/user/myfolder/CLMcom/HadCruz/run1/paris/paris1.nc", "/home/user/myfolder/CLMcom/HadCruz/run1/paris/paris2.nc",
       "/home/user/myfolder/CNRM/CLMcom/rcp85/run1/helsinki/helsinki1.nc")

nchar(paths)
[1] 47 47 55 55 63

As you can see, the vector path does have different size objects.

I want to obtain a new vector with all the objects from paths but without the file names from inside, something like:

> new_paths
[1] "/home/user/myfolder/ERA/amsterdam/"                   
"/home/user/myfolder/ERA/amsterdam/"                  
[3] "/home/user/myfolder/CLMcom/HadCruz/run1/paris/"       
"/home/user/myfolder/CLMcom/HadCruz/run1/paris/"      
[5] "/home/user/myfolder/CNRM/CLMcom/rcp85/run1/helsinki/"

I need this because I want to create a loop where for each i in the vector new_paths, I will run a terminal command using system

I know there is a possibility with strsplit however, I am not expert in manipulating file names in R. Anyone knows a possibility, please?

Upvotes: 1

Views: 256

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388982

For this you can use function dirname directly

dirname(paths)

#[1] "/home/user/myfolder/ERA/amsterdam"                  
#[2] "/home/user/myfolder/ERA/amsterdam"                  
#[3] "/home/user/myfolder/CLMcom/HadCruz/run1/paris"      
#[4] "/home/user/myfolder/CLMcom/HadCruz/run1/paris"      
#[5] "/home/user/myfolder/CNRM/CLMcom/rcp85/run1/helsinki"

Upvotes: 5

Michael Bird
Michael Bird

Reputation: 783

You can use gsub() to replace pattern in strings. By setting the replacement to an empty string you can effectivly remove a match from a string. So

gsub(x = paths, pattern = "[^/]+$", replacement = "")

would remove the last part of the path after the last /. The regex "[^/]+$" matches anything that isn't a / until the end of the string. We then replace that match with ""

Upvotes: 3

Related Questions