Basic Workflow

The R environment is broken up into two main windows, the console and the script. The console window is the place where R is waiting for you to tell it what to do, and where it will show the results of a command. > mark that R is ready to take a command. + means the command is not complete, like you are missing a ) or }. You can type commands directly into the console, but they will be forgotten when you close the session.The script is a simple text (.R) file that stores your code. The point of a well constructed script is not just to “do stuff” but to do it in a way that maintains a complete record of your work so anyone can easily and exactly replicate your workflow and results.

Basic Operation

Control Flow

This will print:

	1
	2
	3
	4
	5

Functions

In R, the last executed line of a function is automatically returned, otherwise use return() to be sure you know what the function is giving back to you.

increment_me(4), will return 5

increment_me(4, 6), will return 10

Packages

Math

Do math by simply typing or pasting in the console.

x+y
x*y
x**y
sum(vector)
mean(vector)
round(vector, decimal_places)

Scientific Commands

Finding Help

Don’t be defeated by a coding problem, semantics confusion, or error messages. Find help:

help(function) or ?function - Input any function into the parentheses for useful syntax and function information. args() gives you the arguments of a function.

You can also check out the resources below or run a general engine search (i.e., r split character string). The hardest part here is finding the right keywords to use.

General Resources

Manual Directories

R Community Forums

How to ask for help

###Style Guides

This document benefited greatly by the inclusion of Data Carpentry materials (Before we start, Introduction to R) and Software Carpentry’s (Programming with R Reference)