how to change the directory of

I have a couple of questions about this:

  1. How can I access this file on r without changing the directory ?
    2)How do I change the directory of a file in r

To identify the file, use a full pathname. Assuming the target file is in a folder in the home directory called Downloads

bookings_df <- read.csv("/Users/ravi/Downloads/hotel_bookings.csv")

Although it is possible to move files around from within R, it's more convenient to put them where you can readily find them without having to give full pathnames or changing your working directory. For a simple project, simply use the ordinary OS tools to move the files you will be working with to the working directory for the course or module or project. Then the original code will work.

For more complicated cases where there may be a lot of scripts, it's convenient to create an RStudio project in a new directory and create two folders there, data and R. Put all the csv files in the first and all the scripts in the second. Install the {here} package and you will have an easy-to-remember consistent way of loading data whether you are in the project directory, a subdirectory or a folder within the subdirectory.

df <- read.csv(here("data/hotel_bookings.csv"))

which relieves you of the full filename nuisance, having everything in a single directory and not being able to find it nuisance and my bane, remembering if it is /data/hotel_booking.csv or data/hotel_booking.csv (no leading slash).

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.