Organize Your Data Files

Before importing data, give the file a clear place in your project. In this example, you will download one small text file and locate it using a relative path. We will reuse it on the R Syntax and Help page.

Download and organize the example

  1. Open your existing RStudio course project using its .Rproj file.
  2. Create a folder named Data inside the project folder if it does not already exist.
  3. Download example.txt and save it in Data/, keeping the name example.txt. If your browser displays the text, save that file using Save As. Move it from your Downloads folder into Data/ if necessary.

Common project layout

Use the following layout as the course progresses. Create each folder when you first need it. The project name and the example document name analysis.Rmd will be your own:

course-project/
├── course-project.Rproj
├── analysis.Rmd
├── analysis.html
├── Data/
│   └── example.txt
├── Scripts/
├── Output/
└── figures/
Location Role
Project folder Keep your .Rmd documents beside the .Rproj; their knitted HTML files are saved here too.
Data/ Downloaded source files and prepared inputs used by the examples. Preserve the source files.
Scripts/ Reusable functions saved in .R files and loaded with source().
Output/ Analysis tables and other results generated by your code.
figures/ Plot files created by the R Markdown template when knitting.

In Unit 03, you will create Data/destatis-land-use-2024-states.rds as a prepared input for the following exploration page. It stays in Data/ whether you generate it yourself or use the alternative prepared download. The CSV export and the cleaned analysis tables in Unit 04 go into Output/.

Inspect the practice file

The file contains a header and three invented observations for practicing imports. The heights are in meters:

tree;height_m
beech;18,5
oak;21,0
pine;16,2

Each line is one row. Semicolons separate the columns, and commas mark decimals. The .txt extension tells you this is a text file; its contents determine the import settings.

Understand the working directory and relative path

The working directory is the folder R uses as the starting point for relative file paths. Opening your RStudio project sets it to the project folder. Check it with:

getwd()

The path Data/example.txt means: start in the working directory, enter Data, and find example.txt. The slash separates the folder name from the filename. This relative path can stay the same when another person opens their copy of the project on another computer.

Keep the project folder as the working directory. The Data/ part of the path already tells R where to look inside it.

When you use these paths in an R Markdown document, save the .Rmd in the project folder, alongside the .Rproj file. By default, knitting uses the folder containing the .Rmd as its working directory. This placement lets the same Data/example.txt path work in the console and when knitting. See the R Markdown working-directory guide for other folder arrangements.

Check that R can find the file

file.exists("Data/example.txt")

The result should be TRUE. If it is FALSE, check the working directory, the folder name and the exact filename. In particular, check capitalization and that the file was not saved as example.txt.txt.

You have now prepared the local file. On the next page, we will read it with read.table() and use the help page to understand the call.

Updated: