Learning Objectives

What You Need

You will need a computer with internet access to complete this lesson.

library(lubridate)library(dplyr)

Work With Directories of Files

In the previous lesson, you broke up a single .csv file into a set of csv files - one for each year. However, what if you wanted to modify each of those .csv files in some way? How would you go about it?

One option could be to open each file individually and then manipulate it.

Another option is to generate a list of all .csv files in your directory. Using that list, you can then create a for loop. OR you can also use an R, apply function if you have built the correct functions that you need to process your data.

Generate a List of Files

You can use list.files() to generate a list of files that you’d like to work with. The list.files() function requires 2 arguments:

The path where the files are located on your computer.

The pattern that you’d like to look for in the files. For example, to find all csv files - you’d use *.csv. If you want to find all files with “_precip” in the name you could do that too.

You can do even more now with your data. Let’s loop through each .csv file and

Open the .csv file.

Add a new column to the data.frame that contains the precipitation in mm.

Export the data.frame as a new .csv in a new data directory (data/week-06/outputs/precip_mm/) with a modified file name.

The example below uses the basename() function to grab just the file name (without the path) from the file variable.

a_file<-"data/week-06/precip-2013.csv"# just get the filename without the full pathbasename(a_file)## [1] "precip-2013.csv"# create a new path to the filepaste0("data/week-06/outputs/precip_mm/",basename(a_file))## [1] "data/week-06/outputs/precip_mm/precip-2013.csv"

# print the name of each filefor(fileinall_precip_files){# read in the csvthe_data<-read.csv(file,header=TRUE)%>%mutate(precip_mm=(HPCP*25.4))# add a column with precip in mm# write the csv to a new filewrite.csv(the_data,file=paste0("data/week-06/outputs/precip_mm/",basename(file)))}

Check for and Create Directories with R with ‘If’ Statements

You are closer to the final file output in the format that you want. However, you can’t write out the files to a new directory unless that directory exists.

You can create new directories and test to see if a directory exists in R too using if statements.

An if statement:

Starts with the word, if.

Is followed by the condition that you are testing for in ().

Then the task that you want R to perform follows, in curly braces {}.

If the if statement condition is TRUE then R will perform the tasks in the curly braces.

# an example if statementif(some-condition-exists){perform-some-task}

In your case, you’d like to test to see if a directory exists.

You can use the dir.exists() function to check to see if a directory exists in your working directory or on your computer.

# create an object with the directory namenew_dir<-"data/week-06/outputs/precip_mm/"# does the dir exist?dir.exists(new_dir)## [1] FALSE

However you want to check if the directory doesn’t exist. To do this, use the ! at the beginning of your function.

# does the dir NOT exist?!dir.exists(new_dir)## [1] TRUE

Now, build your if statement.

First, create a directory path that you wish to check for. Use: data/week-06/outputs/precip_mm/.

Condition: Check to see if that directory path (defined in step 1) exists using dir.exists().

Use an if statement to test whether the dir exists or not.

If the dir doesn’t exist, then create the new directory using dir.create(). Use the recursive = TRUE function argument to ensure that R creates not only the prec_mm dir but also the outputs directory.