Class 7: Learning R Language

Computing for Molecular Biology 1

Andrés Aravena, PhD

2 November 2020

R can be used as a calculator

We can write any formula and ask for the result

> 3*3 + 4*4
[1] 25

We can also use functions, like square root

> sqrt(3^2 + 4^2)
[1] 5

How to use functions

Write the function name followed by round parenthesis

The function input goes inside the parenthesis

> abs(-3)
[1] 3

Some functions take several inputs

Use comma to separate multiple inputs

The order is important

For example, logarithm of 3 in base 2 is

> log(3, 2)
[1] 1.584963

What other “buttons” can we use in R?

Today we talk about four keys: MR, M+, M-, MC.

Learning a new Language

beyond English

Basic Rules of R Language

Each phrase in a program is an order, like

“come here”

Phrases are made with nouns, verbs and adverbs

Today we will focus on nouns and one verb

The first verb we need today is assign, written as

<-

Basic Objects

Nouns are names of objects

To handle objects we give them names

We “store” the objects in variables

If we don’t give a name to an object, it is lost for ever

Assignments

Store the result in memory.

  • Like the M button on the calculator

  • Store to use it later

  • Variables are created when we assign a value to them

    > a <- 2
    > a
    [1] 2

Assignments

Store the result in memory.

  • With arrow we assign. There is no output

    > a <- 2
  • Without arrow we look at the value

    > a
    [1] 2

Assign creates new variables

We can only look at a variable that already exists

> b
Error in eval(expr, envir, enclos): object 'b' not found

We try to see a variable that has not been defined

The computer says “that is an error”

Names are case sensitive

In other words, A is not the same as a

> A
Error in eval(expr, envir, enclos): object 'A' not found

Be careful with small and big caps letters

They are different

Names can be long or short

Short name for easy things, long name for important things

  • this_is_a_long_name
  • thisIsAnotherLongName
  • this is not a name

Be coherent, be clear

Names cannon have “space”

Name starts with a letter, then followed by letters, numbers, dots, underscore

  • No - (dash) in the name

  • No   (space) in the name

Use _ instead

Assignment replaces old value with new value

> a
[1] 2
> a <- a + 1
> a
[1] 3
> a <- a + 1
> a
[1] 4

The right-hand side can be complex

We can use any formula in the right-hand side

c <- sqrt(a^2 + b^2)

The right side is evaluated first, then stored into the variable

Alternative

We can also say

a = 2

but it is not recommended

The arrow is more clear

It shows that we take a value and we put it in the variable

An assignment is not an equation

We create variables when we assign values. They do not exist before

This is more clear than

a = a + 1

which does not make sense in math

An assignment is not an equation

It is a command to put new values into a variable

Question

If we do the following steps

a <- 3
b <- 2 * a
a <- 5
b

What are the values of a and b at the end?

When we do not know: NA

Sometimes experiments fail, and we do not get any result

Sometime people do not answer when we ask

Still, we need to register something.

The special value NA represents missing value

It represents a value that we do not know

Summary: <- stores values in memory

  • With arrow we assign. There is no output

    > a <- 2
  • Without arrow we look at the value

    > a
    [1] 2

Mixing R and Markdown

RMarkdown

Summary of Markdown

Text files, edited with a good text editor

  • Empty lines between paragraphs

  • Sections marked with # and space

  • Lists start with +,- or * and space

  • Computer code has three back-ticks before and after

Mixing R and Markdown

Follow the three back-ticks with {r}

```{r}
2 + 3 * 4
```

Then the code is executed and the result is inserted in the document

> 2 + 3 * 4
[1] 14

This is the key idea

Mixing code and text help us to write documents that adapt to the data

All tables and figures represent the data

If you get new data, the document changes

RMarkdown reports are Replicable

and that is essential