# Basics of R

## Exercices values

Find the solution to these exercises without using R.

Exercise 1: Write the output.

``````cent <- 100
cent <- 50
cent
``````

50

Exercise 2: Write the output.

``````name <- 100
name + 50
``````

150

Exercise 3: Write the output.

``````hello <- 6
Hello <- 4
hello
``````

6

Exercise 4: Write the output.

``````one <- 5
two <- 6
three <- 1
one + two - three
``````

10

Exercise 5: Write the output.

``````LOL <- 100
Lol <- 30
lol
``````

Exercise 6: Write the output.

``````trump <- "Biden"
trump
``````

“Biden”

Exercise 7: Write the output in the console.

``````"SPAIN" == "Spain"
"FRANCE" == "FRANCE"
``````

FALSE TRUE

Exercise 8: Write the output.

``````class(TRUE)
``````

“logical”

Exercise 9: Write the output.

``````"TRUE" == "FALSE"
``````

FALSE

Exercise 10: Write the output.

``````"TRUE" == "true"
``````

FALSE

## Exercices vectors

Find the solution to these exercises without using R.

Exercise 1: Write the output.

``````4:8
``````

4 5 6 7 8

Exercise 2: Write the output.

``````length(2:9)
``````

8

Exercise 3: Write the output.

``````3 * c(3, 4)
``````

9 12

Exercise 4: Write the output.

``````w <- 5
W <- 3
vector <- c(w, W, 1, w)
vector
``````

5 3 1 5

Exercise 5: Write the output.

``````c(30, 40, 50, 60, 70) / 10
``````

3 4 5 6 7

Exercise 6: Write the output.

``````c(3, 5, 8:10) - 1:5
``````

2 3 5 5 5

Exercise 7: Write the output.

``````first <- c(30, 40, 50, 60, 70)
second <- c(3, 4, 5, 6, 7)
first / second
``````

10 10 10 10 10

Exercise 8: Write the output.

``````e <- 2
c(6, 4, 2) / c(e, e, e)
``````

3 2 1

Exercise 9: Write the output.

``````c("Argentina", "Brazil", "Peru") / 3
``````

A character vector cannot be divided by a number!!!

Exercise 10: Write the output.

``````class(c("FALSE", "FALSE", "TRUE"))
``````

“character”

Exercise 11: Write the output.

``````age <- c(24, 40, 85, 12, 59)
age == 50
``````

FALSE FALSE FALSE FALSE FALSE

Exercise 12: Write the output.

``````as.Date("2021/02/23") - as.Date("2021/02/21")
``````

Time difference of 2 days

Exercise 13: Write the output.

``````classes <- c(class("D"), class(1), class(TRUE))
classes
``````

“character” “numeric” “logical”

Exercise 14: Write the output.

``````classes <- c(class("D"), class(1), class(TRUE))
class(classes)
``````

“character”

Exercise 15: Write the output.

``````c(1) == 1
``````

TRUE

Exercise 16: Solve the problem.

``````c(4, 6, 7, 8
``````

it lacks the last parenthesis `)`

Exercise 17: Solve the problem.

``````c(TRUE, FALSE TRUE)
``````

it lacks a comma between the second and the third value.

Exercise 18: Solve the problem.

``````c("France", "Spain", "UK)
``````

it lacks the `)` after the `UK`.

Exercise 19: Solve the problem.

``````as.factor("France", "Spain", "UK")
``````

a character vector must be marked with `c()`

## Exercises dataframe

Exercise 1: Write the output (only the value(s)).

``````holi <- tibble(n = 1:5, v = 6 - 1:5)
holi[1,2]
``````

5

Exercise 2: Write the output (only the value(s)).

``````holi <- tibble(n = 1:5, v = 6 - 1:5)
holi[2, "n"]
``````

2

Exercise 3: Write the output (only the value(s)).

``````holi <- tibble(n = 1:5, v = 6 - 1:5)
holi[3, "v"]
``````

3

Exercise 4: Write the output (only the value(s)).

``````holi <- tibble(n = 1:5, v = 6 - 1:5)
holi\$v
``````

1

Exercise 5: Write the output.

``````holi <- tibble(n = 1:5, v = 6 - 1:5)
holi\$n == 1
``````

TRUE FALSE FALSE FALSE FALSE

Exercise 6: Write the output.

``````holi <- tibble(n = 1:5, v = 6 - 1:5)
holi\$v == 1
``````

TRUE FALSE FALSE FALSE FALSE

Exercise 7: Write the output.

``````holi <- tibble(n = 1:5, v = 5 - 1:5)
dim(holi)
``````

5 2

Exercise 8: Solve the problem.

``````tibble(country = c("Germany", "Argentina", "China"),
lifeexp = c(81.2, 76.2, 73.1)
``````

it lacks the last parenthesis `)`

Exercise 2: Solve the problems.

``````country <- c("Germany". "Argentina", "China"),
lifeexp <- c(81.2, 76.2, 73.1)
tibble(country, lifexp)
``````

a point separates Germany and Argentina
the comma at the end of the first line should be removed in the last row, it lacks an ‘e’ in lifexp

## Exercises selection

Exercise 1: Write the output.

``````c(TRUE, FALSE, TRUE)
``````

FALSE

Exercise 2: Write the output.

``````numbers <- c(10,6,12,17,5)
numbers[3,5]
``````

12 5

Exercise 3: Write the output.

``````numbers <- c(3,22,4,23,5)
numbers[2:3]
``````

22 4

Exercise 4: Which rows and columns do we select?

``````dataframe[c(1,4), ]
``````

1st and 4th row, all columns

Exercise 5: Which rows and columns do we select?

``````dataframe[ ,"country"]
``````

All rows, the column named “country”

Exercise 6: Which rows and columns do we select?

``````dataframe[country == "Argentina", ]
``````

The rows that have Argentina in the country vector.

## Exercises functions

Exercise 1: Write the output.

``````hello <- function(x) {x + x}
hello(3)
hello(x = 6)
``````

6 12

Exercise 2: Write the output.

``````aa <- function(hello = 4) {hello + 1}
aa()
``````

5

Exercise 3: Write the output.

``````bb <- function(hello, bye = 0) {hello + bye}
bb(hello = 3)
``````

3

Exercise 4: Write the output.

``````cc <- function(hello, bye = 0) {hello + bye}
cc(3, 3)
``````

6

Exercise 5: Write the output.

``````dd <- function(x, y) {x * y}
dd(1, 3)
``````

3

Exercise 6: Write the output.

``````ee <- function(x, y = 1) {x * y}
ee(1)
``````

1

Exercise 7: Write the output.

``````numbers <- c(3.55, 4.99, 5.62)
round(numbers)
``````

4 5 6

Exercise 8: Write the output.

``````max(c(3.55, 4.99, 5.62))
``````

5.62

Exercise 9: Write the output.

``````max(sum(c(3.5, 6.5)))
sum(max(c(3.5, 6.5)))
``````

10 6.5

Exercise 10: Solve the problem.

``````round(sum(vector)
``````

it lacks the last parenthesis `)`

Exercise 11: Write the output in the console.

``````char <- c("C", "B", "A", "B", "B", "A")
unique(char)
``````

C B A

Exercise 12: Write the output in the console.

``````char <- c("C", "B", "A", "B", "B", "A")
sort(unique(char))
``````

A B C

Exercise 13: Write the output.

``````as.numeric(c(FALSE, TRUE))
``````

0 1

Exercise 14: Write the output.

``````as.integer(6.2)
``````

6

Exercise 15: Write the output.

``````as.integer(factor(c("Argentina", "Zambia", "Congo")))
``````

1 3 2

## Exercises coercion

Exercise 1: Write the output.

``````c(TRUE, 4)
``````

1 4

Exercise 2: Is this producing a character, numeric or logical vector?

``````c(TRUE, 4)
``````

numeric vector

Exercise 3: What is the output of the following code?

``````c(FALSE, "One", 2)
``````

“FALSE” “One” “2”

Exercise 4: Does this produce a character, numeric or logical vector?

``````c(FALSE, "One", 2)
``````

character vector

Exercise 5: Write the output.

``````end <- c(5, 9, 3)
as.numeric(end > 6)
``````