Friday, March 6, 2020

Blog 14: Set Working Directory, Folders, files

Setting up Working Directory and Creating Folder Structure


Introduction

In this Blog we will look at how to set up working directories in R. Before starting any project it is important to create the directory so that all the results are stored in that location.Specifically we will look at how to check the following

  • Check Default Working Directory
  • Set a new working directory
  • Go one level up in the root directory
  • Create New folder
  • List files in a folder
  • Delete files from a folder
  • Copy/Move files between Folder
  • Open a file physically using R


Installing the library: dplyr,tidyr and stringr

if(!require("dplyr")){
  
  install.packages("dplyr")
}else{
  
  library(dplyr)
}

if(!require("tidyr")){
  
  install.packages("tidyr")
}else{
  
  library(tidyr)
}

if(!require("stringr")){
  
  install.packages("stringr")
}else{
  
  library(stringr)
}


Getting the Current Working Directory

It is important to know the location of current working directory so that you can locate subfolders, files,etc

getwd()
[1] "C:/Users/Dell/Downloads"


Changing the Current Working Directory

Let us set the working directory to Documents folder

setwd("C:/Users/Dell/Documents")


Go one level up in the Root/Parent Directory

We will set the current working directory to Documents. We will then go one level up so that the working directory becomes Dell

setwd("C:/Users/Dell/Documents")
setwd('..')
getwd()
[1] "C:/Users/Dell"


List of All the files in a folder(or a Working directory)

lsfiles<-list.files(getwd())
lsfiles
 [1] "Blog-14-Setting-up-Directory-and-Folders.html"
 [2] "Blog-14-Setting-up-Directory-and-Folders.Rmd" 
 [3] "Blog-14-Setting up Directory and Folders.Rmd" 
 [4] "ChromeSetup.exe"                              
 [5] "desktop.ini"                                  
 [6] "Docs"                                         
 [7] "Downloaded Python R Codes"                    
 [8] "Excel files"                                  
 [9] "Pics"                                         
[10] "PPTS"                                         
[11] "warpbreaks.csv"                               


List of All the csv files

lscsvfiles<-list.files(getwd(),pattern = ".csv")
lscsvfiles
[1] "warpbreaks.csv"


Create a new folder

Here first we will check if the folder already exists and if it doesnt, we will create a new one

if(!dir.exists("C://Users//Dell//Documents//Dummy")){
  
  dir.create("C://Users//Dell//Documents//Dummy")
  
}else{
  
  temp<-"Do Nothing"
}


Delete Files from a Folder

Lets delete the file named “warpbreaks - Copy.csv” from the Downloads folder

setwd("C://Users//Dell////Downloads")
file.remove("warpbreaks - Copy.csv")
[1] FALSE


Copy a file from one folder to another

Lets copy the file “warpbreaks.csv” from Downloads to Documents. Here we will do the following things:

  • Check if “warpbreaks.csv” exists in Documents
  • move the file from DOwnloads to Documents
  • Check again to see if “warpbreaks.csv” has been copied in Documents
  • copy the same file from Documents into Downloads
if(!require("filesstrings")){
  
  install.packages("filesstrings")
}else{
  
  library(filesstrings)
}

file.exists("C://Users//Dell//Documents//warpbreaks.csv")
[1] TRUE
# It gives a False

file.move( "C://Users//Dell//Downloads//warpbreaks.csv",
        "C://Users//Dell//Documents")

# A TRUE indicates that the file has been moved
file.exists("C://Users//Dell//Documents//warpbreaks.csv")
[1] TRUE
file.copy( "C://Users//Dell//Documents//warpbreaks.csv",
        "C://Users//Dell//Downloads")
[1] FALSE


Open the file physically

Lets open the file named warpbreaks.csv from the Downalods folder

file.show("C://Users//Dell//Documents//warpbreaks.csv")


Final Comments

We saw how we can set working directories, create folders and move/copy files.These normally come in handy while working on a Data set


Web Scraping Tutorial 4- Getting the busy information data from Popular time page from Google

Popular Times Popular Times In this blog we will try to scrape the ...