02. Your first R session

When you first run R, you will be engaged in its interactive mode. This means that with each command inputted, R will interpret it and display the results to you. This occurs all within a command line interface, or the console in RStudio.

Hello world!

Let's begin your very first R session! Open up RStudio and jump into your console. You should be prompted with a > symbol. This is where you type in your commands. Try typing in the following:

> print("Hello world!")
[1] Hello world!

See what you did here? You told R to print the words "hello world!" which it did. By default, R prints back any variable inputs, so the print() function is extraneous.

> "Hello world!"
[1] Hello world!

Positional values with [1]

Notice the [1] on the side. This refers to the numerical position of the data being printed out. Here, it means the "Hello world!" text is the first output. It's seems unnecessary for this case, but take, for example, an instance where you output 42 numbers:

> seq(1,42)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

The example above uses the seq() function, which generates a regular sequence. We can see that the [26] marks the 26th position of the sequence.

Errors in R

If you place some function or command that R does not recognize, it'll return an error.

> asdf
Error: object 'asdf' not found

Setting variables with <-

In R, just as with any other programming language, we can assign values to a declared variable with the <- symbols. For example, let's place the number 5 in the variable x.

> x <- 5
[1] 5

Now any time in your current session, you can call x to whatever function you want, and it'll translate it to the number 5.

Environment Objects

Notice how in your Environment panel, under Global Environent, x is there. This means that the variable is now part of your session or environment. You can clear or remove any number of objects you created within this panel.

RStudio's Environment section will be updated whenever you put in a new variable.
RStudio's Environment section (upper-right) will be updated whenever you put in a new variable.

Another way to check your environment objects solely through the console is with the ls() function.

> ls()
[1] "coinflip" "dieroll" "x"

You can also remove any variables with the rm() function.

> rm(x)
# Can't use x anymore

R is CaSe-SeNsItIvE!

Note that R is case-sensitive, meaning that X is the not the same as x.

Commenting with #

Commenting is done using the pound symbol (#). Anything code proceeding this will be ignored by the R interpreter. This will be useful when you start writing R scripts, and need to explain what each piece of code's purpose is.

> # This does nothing

Entering small datasets

The c() function is the most used function for small datasets. It combines or concatenates elements together. Think of these as a one-dimensional vector.

> coinflip <- c("heads", "tails")
> dieroll <- c(1,2,3,4,5,6)

Navigating the R console history

Make use of the up and down arrow keys to go back or forwards in your history list. For RStudio, you may view your R history with the history panel (top right).

R History panel. Double click a command to reload it.
R History panel. Double click a command to reload it.

Example R datasets

R comes packages with example datasets so you can learn and practice R functions. To view a list of datasets, use the data() command.

> data()

If you're using RStudio, the page of example datasets will open in an internal window. Let's try the LakeHuron dataset, which includes a measure of the level of LakeHuron from 1875 to 1973.

> LakeHuron 
Time Series:
Start = 1875 
End = 1972 
Frequency = 1 
 [1] 580.38 581.86 580.97 580.80 579.79 580.39 580.42 580.82 581.40
[10] 581.32 581.44 581.68 581.17 580.53 580.01 579.91 579.14 579.16
[19] 579.55 579.67 578.44 578.24 579.10 579.09 579.35 578.82 579.32
[28] 579.01 579.00 579.80 579.83 579.72 579.89 580.01 579.37 578.69
[37] 578.19 578.67 579.55 578.92 578.09 579.37 580.13 580.14 579.51
[46] 579.24 578.66 578.86 578.05 577.79 576.75 576.75 577.82 578.64
[55] 580.58 579.48 577.38 576.90 576.94 576.24 576.84 576.85 576.90
[64] 577.79 578.18 577.51 577.23 578.42 579.61 579.05 579.26 579.22
[73] 579.38 579.10 577.95 578.12 579.75 580.85 580.41 579.96 579.61
[82] 578.76 578.18 577.21 577.13 579.10 578.25 577.91 576.89 575.96
[91] 576.80 577.68 578.38 578.52 579.74 579.31 579.89 579.96

We can find the mean, and standard deviation of this dataset.

> mean(LakeHuron)
[1] 919.35
> sd(LakeHuron)
[1] 169.2275

We may also plot a histogram. The result will show in your Plots tab.

> hist(LakeHuron)
Graphical figures are generated out in RStudio.
Graphical figures are generated out in RStudio.

To save this image, click on Export and you may download it as a PDF or an image.

Learn to be a Pythonista!

Python Programming

Learn to be a Pythonista! Try Python

This book is designed to be used as the primary textbook in a college-level first course in computing. It takes a fairly traditional approach, emphasizing problem solving, design, and programming as the core skills of computer science. However, these ideas are illustrated using a non-traditional language, namely Python.

$ Check price
45.9945.99Amazon 4.5 logo(211+ reviews)

More Python resources

Become a Bioinformatics Whiz!

Introduction to Bioinformatics Vol. 2

Become a Bioinformatics Whiz! Try Bioinformatics

This is Volume 2 of Bioinformatics Algorithms: An Active Learning Approach. This book presents students with a light-hearted and analogy-filled companion to the author's acclaimed course on Coursera. Each chapter begins with an interesting biological question that further evolves into more and more efficiently solutions of solving it.

$ Check price
49.9949.99Amazon 5 logo(5+ reviews)

More Bioinformatics resources

Ad