Basics of R

Exercices values

Find the solution to these exercises without using R.

Exercise 1: Write the output in the console.

cent <- 100
cent <- 50
cent
Answer

50

Exercise 2: Write the output in the console.

name <- 100
name + 50
Answer

150

Exercise 3: Write the output.

hello <- 6
Hello <- 4
hello
Answer

6

Exercise 4: Write the output.

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

10

Exercise 5: Write the output in the console.

LOL <- 100
Lol <- 30
lol
Answer

Error: object ‘lol’ not found

Exercise 6: Write the output in the console.

trump <- "Biden"
trump
Answer

“Biden”

Exercise 7: Write the output in the console.

"SPAIN" == "Spain"
"FRANCE" == "FRANCE"
Answer

FALSE TRUE

Exercise 8: Write the output in the console.

class(TRUE)
Answer

“logical”

Exercices vectors

Find the solution to these exercises without using R.

Exercise 1: Write the output in the console.

4:8
Answer

4 5 6 7 8

Exercise 2: Write the output in the console.

length(2:9)
Answer

8

Exercise 3: Write the output in the console.

3 * c(3, 4)
Answer

9 12

Exercise 4: Write the output in the console.

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

5 3 1 5

Exercise 5: Write the output in the console.

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

3 4 5 6 7

Exercise 6: Write the output in the console.

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

10 10 10 10 10

Exercise 7: Write the output in the console.

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

A character vector cannot be divided by a number

Exercise 8: Write the output in the console.

age <- c(24, 40, 85, 12, 59)
age > 50
Answer

FALSE FALSE TRUE FALSE TRUE

Exercise 9: Write the output in the console.

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

Time difference of 2 days

Exercise 10: Write the output in the console.

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

“character” “numeric” “logical”

Exercise 11: Write the output in the console.

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

“character”

Exercises dataframe(#dataframe)

Exercise 1: Solve the problem.

data.frame(country = c("Germany", "Argentina", "China"),
           lifeexp = c(81.2, 76.2, 73.1)
Answer

it lacks the last parenthesis )

Exercise 2: Solve the problem(s).

country <- c("Germany". "Argentina", "China"),
lifeexp <- c(81.2, 76.2, 73.1)
data.frame(country, lifexp)
Answer

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

Exercise 3: Write the output.

c(TRUE, 4)
Answer

1 4

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

c(TRUE, 4)
Answer

numeric vector

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

c(FALSE, "One", 2)
Answer

“FALSE” “One” “2”

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

c(FALSE, "One", 2)
Answer

character vector

Selection

Exercise 1: Write the output.

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

12 5

Exercise 2: Write the output.

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

22 4

Exercise 3: Which rows and columns do we select?

dataframe[c(1,4), ]
Answer

1st and 4th row, all columns

Exercise 4: Which rows and columns do we select?

dataframe[ ,"country"]
Answer

All rows, the column named “country”

Functions

Exercise 1: Write the output.

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

6 12

Exercise 1: Write the output.

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

4 5 6

Exercise 2: Write the output.

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

5.62

Exercise 3: Write the output.

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

10 6.5

Exercise 4: Solve the problem.

round(sum(vector)
Answer

it lacks the last parenthesis )

Exercise 5: Write the output in the console.

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

C B A

Exercise 6: Write the output in the console.

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

A B C

Exercise 7: Write the output.

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

0 1

Exercise 8: Write the output.

as.integer(6.2)
Answer

6

Exercise 9: Write the output.

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

1 3 2

Exercise 10: Write the output.

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

0 1 0

Previous
Next