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.
x
extract numbers > 10 using
which()
function.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"
.
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
.
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
.
==,>,<, >=, <=
operators create logical
vectors. Check the results of the following operations:
myvec=1:5
, myvec > 3
,
myvec == 4
, myvec <= 2
,
myvec != 4
1, TRUE, FALSE, 25 and 4
. Explain what happens.1, 3, "a", "b" and "c"
. Explain what happens.1, 3, "a", "b", "c" and FALSE
. Explain what happens.w
into a factor.Define a specific order (choose the order yourself), then order the
names in w
based on the assigned levels.
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?
nums
.Then, create a new logical vector indicating the numbers in
nums
satisfy at least one of the following conditions:
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"
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
.