1 Goal


The goal of this tutorial is to avoid writing by hand a path if we want to use data from different folders in the same script. In this example we will recreate the case of reading 3 tables from three different folders.


2 Setting the paths

2.1 One file per folder


# Let's think we have 3 tables in 3 different folders and we don't want to write several times the path by hand
# We can start storing the paths in variables 

path1 <- "/Users/Ubiqum/data/November"
path2 <- "/Users/Ubiqum/data/October"
path3 <- "/Users/Ubiqum/data/September"

# Now we can name the files in question

file1 <- "Dataset.csv"
file2 <- "table.csv"
file3 <- "september2017.csv"

# We can use paste0 to merge the paths with the filenames

paste0(path1, "/", file1)
## [1] "/Users/Ubiqum/data/November/Dataset.csv"
paste0(path2, "/", file2)
## [1] "/Users/Ubiqum/data/October/table.csv"
paste0(path3, "/", file3)
## [1] "/Users/Ubiqum/data/September/september2017.csv"

2.2 Several files per folder


# We can imagine that we have several files in the same folder so storing paths in a variable will save a lot of effort

paste0(path1, "/", file1)
## [1] "/Users/Ubiqum/data/November/Dataset.csv"
paste0(path1, "/", file2)
## [1] "/Users/Ubiqum/data/November/table.csv"
paste0(path1, "/", file3)
## [1] "/Users/Ubiqum/data/November/september2017.csv"

3 Conclusion


In this tutorial we have learnt how to create paths merging folder paths with filenames. Storing paths into variables is a clean way to reduce mistakes and see which files are we actually reading.