ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

R Programming Notes on the Working Directory and Files

Updated on February 10, 2013
Source

(c) 2013 kevin languedoc (klanguedoc)

R is programming language for statistical computing and visualization. R is based on the S language which is another statistical language development at Bell Laboratories. R is an open source project and is free to use is governed by the GNU license. You can download R from the R Project web site.


R provides an extensive API for programming statistical data analysis and graphical representation. You can perform linear and nonlinear data modelling and analysis. Classical statistical test can be performed as well as time series analysis, classification and clustering. Data be cleansed and subsetted from a variety of sources including open data data sets. R is highly extensible, allow developers to create new statistical functions. Finally R can be used to graphically represent your data and the results of your analysis in graphical plots and graphs.

How To Install R

R is organized into R packages which are stored in the Comprehensive R Archive Network or CRAN. From the R Project site at www.r-project.org click on the download R link or the CRAN and select an appropriate CRAN mirror.

Select the Windows or Mac binaries depending on your operating system. Launch the installer and follow the instructions like any other program. The R platform comes with an editor can be used to execute queries, write functions that can be saved and distributed or used in your scripts. You can perform all R operations from the integrated IDE or you can download one of the R development IDEs available on the market.

How To Install R Studio

R Studio is also available as a free download from http://www.rstudio.com/. R Studio is a comprehensive development environment. The IDE offers some interesting features like being to integrate R with C/C++ programs and also to create R Sweave script which makes it possible to integrate R in LaTex documents.

R Studio IDE
R Studio IDE | Source

How To Set Your Working Directory

The Working directory is the directory that R uses to load its data and other files and also to save its scripts and analysis results. You can see your default working directory by typing the getwd() command in the R Console or by selecting the “Get Working Directory” command under the Misc menu on a Mac and Windows. The default is the user path on both Mac and Windows. To set a new path or a subdirectory in the default in R you will need to create the directory through the operating system’s file manager which is Windows Explorer or Finder.

To set the Working Directory, select the “Set Working Directory” under the Misc menu or use the setwd(path/to/your/directory) on Mac or setwd(c:\\path\\to\\your\\working\\directory) on Windows as the following demonstrates. You can also use the relative path like this : setwd(“./directory”) which is relative to the current working directory path. You will probably notice that these commands are similar to Unix and DOS commands

It is important to create a specific folder (directory) and not store everything in the default working directory because you will lose control over your contents as it gets mixed in with files from other programs.

Getting and Setting the R Working Directory
Getting and Setting the R Working Directory | Source
Source

List of Files in your Working Directory
Use dir() to get a listing of files in a directory. If the directory is empty, the console will output character(0) meaning that there is no files.


dir()

output with no files:
character(0)

output with files:
dir()
[1] "names.csv"


Another useful method to list files in a directory is to use the list.files() function. Likewise the list.dir() performs a similar operation by listing all the sub-directories or directories at a given path. The list.files and list.dir has several parameters which described below:

Create Directories and Sub-Directories
To create a directory you can use the dir.create() function. This function has three arguments: path, showWarnings and recursive. The arguments definitions are provided below:

  • path: The location where you want to create the directory
  • showWarnings: This is a boolean parameter
  • recursive: This is also a boolean value as TRUE or FALSE. Setting this value indicates if elements other can the first should be created.

Another useful method to list files in a directory is to use the list.files function. Likewise the list.dir performs a similar operation by listing all the sub-directories or directories at a given path. The list.files and list.dir has several parameters which described below:

  • path = This is the relative or absolute path
  • pattern = This is a regular expression to help filter the contents
  • all.files = FALSE or TRUE. If TRUE then the all files are visible even the hidden files
  • full.names = Specify FALSE if you don’t want to display the full names
  • recursive = TRUE OR FALSE to list files or directories recursively
  • ignore.case = TRUE to ignore case or FALSE to distinguish between upper and lowercase words
  • include.dirs = Specify TRUE to include the “.” and “..” directories

These are some of the functions that I used frequently when working with R. However there hundreds if not thousands of functions. R has a very rich API that is worth learning and will become a mainstream language as we move deeper and deeper into advance analytics and data analysis.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)