Here is a set of 14 tasks in the R language that will allow you to delve into various aspects of working with vectors and functions in this versatile programming language:

Create a 7-element vector named “results” with the following numerical values: 24, 22, 15, 21, 15, 17, 10.

results = c(24, 22, 15, 21, 15, 17, 10)

Name the elements of the vector with the names of the days of the week.

names(results) = c("pn", "wt", "śr", "czw", "pt", "sob", "nd")

Select the sixth and seventh elements of the vector “results” by referring to their names and store them in the vector “weekend”.

weekend = results[c("sob","nd")]

What is the minimum, maximum, and mean value of the vector “results”? Use the appropriate functions.

min(results)
max(results)
mean(results)

Create a 10-element vector W1 consisting of even numbers from 2 to 20.

W1 = seq(from=2, to=20, by=2)
W1 = seq(2, 20, by=2)

Create a 12-element vector W2 from the repeating sequence of numbers 4, 8, and 12.

W2 = rep(c(4,8,12), times=4)
W2 = rep(c(4,8,12), each=4)

Combine vectors W1 and W2 into one vector W3.

W3 = c(W1, W2)

Refer to (display):

a) To reference the 12th element of vector W3:

 W3[12]

b) To reference the 2nd, 6th, and 9th elements of vector W3:

 W3[c(2,6,9)]

c) To reference the first six elements of vector W3:

W3[1:6]

Create a 20-element vector W4 consisting of randomly chosen numbers from the range 1 to 100.

W4 = sample(1:100, 20)

Create a vector W5 which is the sum of vector W3 and vector W4 raised to the second power.

W5 = W3 + W4^2

Display the minimum elements from corresponding elements of vectors W3 and W4.

pmin(W3, W5)

Which elements of vector W5 are greater than 5000?

which(W5>5000)

Create a vector W6 containing the integer part of dividing elements of vector W5 by 17.

W6 = W5 %/% 17 
W6 = W5 %% 17 

Sort the elements of vector W6 in ascending order.

sort(W6)
sort(W6, decreasing = T)

My site is free of ads and trackers. Was this post helpful to you? Why not BuyMeACoffee


Reference:

  1. R (programming language)