R Syntax and Help
Use example.txt, saved in your project’s Data/ folder on the previous page. Keep your project folder as the working directory. Here you will use R’s documentation to choose the import settings for that file and inspect the result.
Open the help page
Enter either of these commands in the R console:
?read.table
help("read.table")
In RStudio, these commands open documentation in the Help pane. The heading identifies read.table and its package, utils; Data Input is the title. Several functions share the page, so find read.table in Usage.
Use the Base R guides for the general foundations:
- R Syntax: read a function call from left to right explains parentheses, argument names and values, quotation marks, commas, and assignment with
<-. - R Help-Files: a quick route through the help connects Usage, Arguments and Value; its default-value guide explains omitted arguments and
....
If these ideas are new, work through those guides first. The syntax lesson supplies its own comma-separated practice file. Return here to apply the same approach to your semicolon-separated Data/example.txt; keep this course’s project and file layout.
Read a function call from left to right
Use the file’s contents to choose the inputs:
read.table(file = "Data/example.txt", header = TRUE, sep = ";", dec = ",")
The result is a data frame with three rows and two columns: tree and height_m.
| Setting | Why it fits this file |
|---|---|
file = "Data/example.txt" |
Gives the path to the local file prepared on the preceding page. |
header = TRUE |
Uses the first line as column names. |
sep = ";" |
Reads semicolons as field separators. |
dec = "," |
Converts heights such as 18,5 into numeric values in R. |
The comma after sep = ";" separates arguments in R code; the comma inside dec = "," describes decimal notation in the file. Keep these two roles apart when adapting an import.
For named and positional arguments, compare the syntax guide with this call: file is named here, and its value is the path to your own course file.
Store and inspect the result
Store the imported table so you can use it in further calls:
trees <- read.table(
file = "Data/example.txt",
header = TRUE,
sep = ";",
dec = ","
)
dim(trees)
str(trees)
dim(trees) returns 3 rows and 2 columns. str(trees) shows that tree contains text and height_m contains numbers. Check that the heights are 18.5, 21.0 and 16.2 meters: the file uses decimal commas, while R prints decimal points. trees <- stores the returned data frame under the name trees.
The next example applies this workflow to a real land-use dataset. It introduces the additional import choices that dataset needs and follows its preparation through to an RDS file.
Exercise: Read the help for read.table()
Use ?read.table to answer these questions before running another import:
- Which argument skips introductory lines, and what is its default?
- Which argument limits the number of data rows read?
- Why does the course example explicitly set
header = TRUEandsep = ";"? - What kind of object does
read.table()return, and where is it stored in the example?
Use the official online help for read.table() if installed help is unavailable. The official help() documentation describes the help system itself.