Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 39 ยท Getting Started

Getting Help and Working Efficiently in R

Learn how to read R help pages, search documentation, understand error messages and warnings, ask good questions, and build habits for clean, reproducible scripts.

Nobody memorizes all of R. Experienced programmers look things up all day. What separates beginners from confident users is knowing how to find answers quickly and how to read what R tells them. This lesson covers the built-in help system, error messages, and a handful of habits that keep your scripts clean.

The built-in help

Every function and dataset has a help page. Open it with a question mark:

R
?mean
help("mean")        # same thing
??regression        # search all help pages for a word

In RStudio the page appears in the Help pane. Click a function name in your script and press F1 for the same effect.

A help page has a predictable layout:

Section What it tells you
Description What the function does, in a sentence or two
Usage The function with all its arguments and default values
Arguments What each argument means
Value What the function returns
Details / Note Special behavior, warnings
See Also Related functions
Examples Code you can run

Beginners often skip to Examples, which is a great habit: run example(mean) to see them execute, or copy them into your script and change them.

Reading the Usage line

R
mean(x, trim = 0, na.rm = FALSE, ...)
  • x has no default, so you must provide it.
  • trim = 0 and na.rm = FALSE have defaults, so they are optional.
  • ... means other arguments can be passed on to other functions.

So mean(c(1, 2, NA), na.rm = TRUE) changes one default. We will return to NA in Data Types and Coercion.

Finding things you do not know the name of

R
apropos("mean")        # functions whose names contain "mean"
example(paste)         # run the examples of a function
args(round)            # just the argument list
str(mtcars)            # compact structure of any object
class(mtcars)          # its type

str() is a lifesaver. When you have an unfamiliar object, str(object) shows its type, size and first values in a few lines.

When the function name is unknown, search the web with the word "R" plus a plain description ("R count missing values in each column"). Good sources are package documentation sites, the vignettes described in R Packages, and Q&A communities. Treat any answer as a lead: run it, understand it, adapt it.

Understanding errors, warnings and messages

R communicates in three levels:

Kind Looks like Meaning
Error Error in ...: R stopped. Fix it.
Warning Warning message: R finished but something may be wrong.
Message plain text, often from packages Information only.

Common errors and what they usually mean:

R
mean(scorez)
#> Error: object 'scorez' not found

A typo, or the variable was never created.

R
Mean(c(1, 2, 3))
#> Error in Mean(c(1, 2, 3)) : could not find function "Mean"

Wrong capitalization, or a missing library() call.

R
"5" + 1
#> Error in "5" + 1 : non-numeric argument to binary operator

Text cannot be added to a number. Convert with as.numeric("5").

R
as.numeric("abc")
#> [1] NA
#> Warning message:
#> NAs introduced by coercion

The conversion failed, so R gave NA and warned. Do not ignore warnings like this one.

Debugging recipe. Read the message from the top. Find the line number. Run the code one line at a time. Check each variable with str() or class(). Most bugs are a value that is not what you assumed.

Asking a good question

When you cannot solve a problem alone, ask a person or a community. A reproducible example gets answers fast: the smallest piece of code that shows the problem, and that others can run.

  1. Remove everything not needed to show the problem.
  2. Use a built-in dataset like mtcars, or create a few rows with data.frame().
  3. Include the exact error text.
  4. Say what you expected to happen.
  5. Show your R version with sessionInfo().

Often, creating the small example reveals the bug before you ever post it.

Habits for clean scripts

Good habits now save hours later.

  • Put library() calls at the top of every script.
  • Comment the why, not the what. # drop test accounts beats # filter rows.
  • Name things clearly with snake_case, as in R Basics.
  • Keep lines short (about 80 characters) and use spaces around operators: x <- a + b.
  • Split long pipelines and long calls over several lines, one argument per line:
R
result <- round(
  mean(scores, na.rm = TRUE),
  digits = 1
)
  • Run from the top. Restart R and run the whole script to prove it works from scratch.
  • Keep raw data untouched. Read it, transform it in code, and write results to separate files.
  • One project per analysis, with folders such as data/, R/, and output/.

RStudio helps: select code and press Ctrl+Shift+A to reformat it, or use the styler package to format whole files automatically. Ctrl+Shift+C comments or uncomments the selected lines.

Your workflow, in one picture

A healthy R workflow

Project One folder per analysis
Script Libraries on top
Run Line by line, then whole
Restart Prove it runs fresh

If it only works in your open session, it is not finished.

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you open the help page for median?

    ?median or help("median") opens the page.
  2. 2. In mean(x, trim = 0, na.rm = FALSE, ...), which argument is required?

    Only x has no default value, so the caller must supply it.
  3. 3. Which function gives a compact overview of an unfamiliar object?

    str() prints the type, size and first values of nearly any object.
  4. 4. What is the difference between a warning and an error?

    An error halts execution. A warning means R completed, but you should check the result.

Summary

  • Use ?function, example(), args() and str() to learn about functions and objects.
  • Help pages follow a fixed layout; the Usage and Examples sections are the most useful.
  • Distinguish errors, warnings and messages, and never ignore warnings.
  • A small reproducible example is the best way to ask for help.
  • Keep scripts tidy: libraries on top, clear names, comments for the why, and test by restarting R.

Next: Vectors