Vectors

  1. Create a vector x using seq() with the odd numbers from 1 to 13.

Use the R function sum() to compute the sum of these numbers.

Use the R function mean() to compute the mean of these numbers.


  1. From x extract numbers > 10 using which() function.

  1. Create a character vector z with the following feminine names: "Giulia", "Maria", "Beatrice", "Roberta", "Barbara".

Then use both grep() and grepl() functions to identify which names contain the letter b.

Which is the difference between their outputs?

Finally, extract from z the names that contain the uppercase letter "B".


  1. Create a vector w containing the names of students in a school class: "Giovanni", "Mario", "Maria", "Luca", "Matteo", "Nadia".

Check if names in vector z (from the previous exercise) are present in w vector.

If there are any matches, extract the indices of those names in w.


  1. Add attributes to the vector w. In particular, add surnames using names() function and then sex and age as attributes.

Then, extract the attribute age and assign it to a new vector a.

Display all attributes of w.


  1. ==,>,<, >=, <= operators create logical vectors. Check the results of the following operations: myvec=1:5, myvec > 3, myvec == 4, myvec <= 2, myvec != 4

  1. Create a new vector that contains 1, TRUE, FALSE, 25 and 4. Explain what happens.

  1. Create a new vector that contains 1, 3, "a", "b" and "c". Explain what happens.

  1. Create a new vector that contains 1, 3, "a", "b", "c" and FALSE. Explain what happens.

  1. Convert the vector w into a factor.

Define a specific order (choose the order yourself), then order the names in w based on the assigned levels.


  1. Random numbers: Random numbers with specific distributions (e.g. uniform or normal) are essential in statistical data analysis.

Look up the description of R functions runif(), rnorm(), mean(), and sd() using the help documentation in R Studio.

The R function runif(10, min=0, max=1) generates 10 random numbers uniformly distributed between 0 and 1. Generate 1000 random numbers uniformly distributed between 0 and 3 and save them in a vector. Compute the mean of these numbers. Is the computed mean close to the expected theoretical mean of a uniform distribution over [0,3]?

The R function rnorm(10, mean=0,sd=1) generates 10 random numbers with mean = 0 and standard deviation = 1. Generate 1000 random numbers with mean = 150, sd = 9 and save them in a vector. Compute the mean and the standard deviation of these numbers. Compare the computed values with the expected theoretical values. Are they reasonably close?


  1. Generate 100 random numbers uniformly distributed between -80 and 120 and assign them to a vector nums.

Then, create a new logical vector indicating the numbers in nums satisfy at least one of the following conditions:

  • The absolute value is greater than 75 OR
  • The value is between 0 and 13

  1. Create a vector t that containing the first 10 multiples of 3.

Assign the following names to t: "Antonio", "Barbara", "Carlo", "Davide", "Enrico", "Fabio", "Giovanna", "Holly", "Irene", "Luca".

Create another vector sp that contains the repetition of c("A", "B") five times.

Assign the followinig names to sp: "Carlo","Luca","Barbara","Enrico", "Fabio", "Giovanna", "Antonio","Holly", "Irene","Davide".

Use the match() function to assign an attribute Number to sp with numbers from t corresponding to the correct person.

Add another attribute Explanation to sp with the text "This is a race"


  1. Create a vector plant containing the words "leaf", "root", "brown", "green".

Create another vector colors containing: “red”, “purple”, the repetition of “green” 3 times, the repetition 8 times of “orange” and “darkbrown”.

Create a new vector matches that contains only the elements in colors that are also present in plant.

Compute and print the length of matches.