01 Check the working directory of R
getwd()
02 Create a folder called “pippo” on the desktop of your computer
dir.create("/Desktop/PIPPO")
03 Move the working directory to the folder created in the previous step
setwd("/Desktop/PIPPO")
04 Check the packages and datasets loaded in the workspace. Is the dataset "women" present?
women
05 Install the package "lmtest" using the command line and then load it
install.packages("lmtest")
library(lmtest)
06 Create a variable called "A" assigning it the value 20
A <- 20
07 Check the class of the variable A
class(A)
08 Create a variable called “A2” and assign it the value “20” (with quotes)
A2 <- "20"
09 Check the class of the variable "A2"
class(A2)
10 Create a variable called “B” and assign it the value “DARE”
B <- "DARE"
11 Create the variable "B2" by copying the variable B
B2 <- B
12 Replace the letter "D" with the letter "M" in the value of the variable B2
sub("D", "M", B2)
13 Delete the variables A, A2, B, and B2
rm(list= ls())
14 Calculate the area of a circle with a radius of 10 cm
2 x 10 x 3.14
15 Calculate the area of a trapezoid considering the larger base, smaller base, and height measuring respectively 10 cm, 4 cm, and 5 cm
(10 + 4) * 5 / 2
16 Create a vector containing the values 1, 3, 5, 9, 17 using the scan function (when you have finished entering the values, press enter twice)
scan()
# enter the numbers one at a time and press enter, finally press enter twice
17 Create a vector containing the values 1, 3, 5, 9, 17 using the "c" operator
vector <- c(1, 3, 5, 9, 17)
18 Create a vector containing the values 1, 2, 3, ..., 499, 500. Do not use the seq function
vector <- c(1:500)
19 Create a vector containing all even numbers from 2 to 500
vector <- seq(2, 500, 2)
20 Create a vector containing the values “a”, “b”, “c”, “d”, and “e”. Each value must be repeated 10 times before moving to the next value
vector <- c(rep("a", 10), rep("b", 10), rep("c", 10), rep("d", 10), rep("e", 10))
21 Create a vector containing the values “a”, “b”, “c”, “d”, and “e” and make sure that the sequence of letters is repeated 10 times
vector <- rep(c("a", "b", "c", "d", "e"), 10)
22 Create a logical vector of 50 elements containing 30 F and 20 T
vector <- c(rep(F, 30), rep(T, 20))
23 Create a dataframe called “nitrati”. The dataframe must contain a column called “citta” and another called “NO3”. The column called “citta” must contain the values “Bologna”, “Milano”, “Ancona”, “Roma”, “Vasto”, “Lecce”, “Cagliari”, “Palermo”; the column called “NO3” must contain the values 4.8, 8.7, 5.5, 7.9, 4.4, 3.6, 3.2, 3.1. Display the dataframe
citta<-c("Bologna", "Milano", "Ancona", "Roma", "Vasto", "Lecce", "Cagliari", "Palermo")
NO3<-c(4.8, 8.7, 5.5, 7.9, 4.4, 3.6, 3.2, 3.1)
nitrati<-data.frame(citta, NO3)
24 Display the cities
citta
25 Extract the cities and display them in alphabetical order
sort(citta)
26 Extract the NO3 value of the city of Palermo
subset(nitrati, citta == "Palermo")[, 2]
27 Sum the NO3 values
sum(nitrati$NO3)
28 Calculate the maximum, minimum, mean, and median of NO3
summary(nitrati$NO3)
29 Display the cities with NO3 < 4
subset(nitrati, NO3 < 4)[,1]
30 Rename the columns of the dataframe converting the column names to uppercase
colnames(nitrati)<-c("NO3", "Citta")
31 Reconvert the columns to lowercase
colnames(nitrati) <- c("no3", "citta")
32 Add an additional column to the dataframe, call it "posizione" and insert the values "nord", "nord", "centro", "centro", "centro", "sud", "isola", "isola"
posizione <-c("nord", "nord", "centro", "centro", "centro", "sud", "isola", "isola")
nitrati<-data.frame(nitrati, posizione)
33 Extract all rows where posizione = "isola" and save them in a new dataframe
DATAFRAME<-subset(nitrati, posizione == "isola")
34 Create two vectors containing respectively the numbers from 1 to 20 and the numbers from 11 to 30. Then create with rnorm two vectors containing each 20 pseudo-random values from a standard normal distribution. Name the four vectors respectively W, X, Y, and Z
W<-c(1:20)
X<-c(11:30)
Y<-rnorm(20)
Z<-rnorm(20)
35 Display the contingency table of W and X
table(W, X)
36 Display the common elements of the vectors W and X
intersect(W,X)
37 Display the elements of W that are not contained in X
subtract(W,X)
38 Verify that the value 35 is not contained in X
setdiff(W,X)
39 Display the positions of the elements greater than 15 in the vector X (use which)
which(X>15)
40 Indicate the position of the minimum value of Z
a <- min(Z)
which(Z == a)
41 Round all values of Y first down and then up
floor(Y)
ceiling(Y)
42 Create a factor vector called “esame” containing 20 times the value “superato” and 30 times the value “nonsuperato”
esame <- c(rep("superato", 20), rep("nonsuperato", 30))
43 Convert the values of “esame” respectively from “superato” and “non superato” to “1” and “0”
esame <- as.factor(esame)
levels(esame) <- c(0, 1)
44 Create a 4x4 matrix with 16 random numbers
x <- rnorm(16)
dim(x) <- c(4, 4)
45 Extract the second row of the matrix and add 10 to the entire row
x[2, ] + 10
46 Extract the third column of the matrix and subtract 4
x[, 3] - 4
47 Double all the values in the matrix
2 * x
48 Create the dataframe "prov" that contains two columns: "citta" containing “Bologna”, “Milano”, “Ancona”, “Roma”, “Vasto”, “Lecce”, “Cagliari”, “Palermo” and "provincia" containing "BO", "MI", "AN", "RO", "CH", "LE", "CA", "PA"
citta <- c("Bologna", "Milano", "Ancona", "Roma", "Vasto", "Lecce", "Cagliari", "Palermo")
provincia <- c("BO", "MI", "AN", "RO", "CH", "LE", "CA", "PA")
prov <- data.frame(citta, provincia)
49 Merge nitrati and prov with merge and create the new dataframe "DATI"
DATI <- merge(nitrati, prov, by.x = "Citta", by.y = "citta", all = T)
50
Use the paste0 function to create a new variable that contains the cities, a space, and in parentheses the abbreviation of the province. Example: Palermo (PA)
paste(prov$citta, "(", prov$provincia, ")", sep = " ")
51 Use the CO2 dataframe and display the variable names:
colnames(CO2)
Select the plants from CO2 with Type="Mississipi". Make sure the plant names appear only once:
unique(subset(CO2, Type == "Mississipi")[, 1])
Select the rows from CO2 where Type="Mississipi" and create a new dataframe including only the columns "Plant", "Treatment", and "conc":
subset(CO2, Type == "Mississipi")[, c(1, 3, 4)]
Use the USArrests dataframe and calculate the mean for each variable using the apply function:
apply(USArrests, 2, mean)
Save the "nitrati" dataframe to a text file called "NITRATI.txt":
write.table(nitrati, "NITRATI.txt", sep = "\t")
Load the file "NITRATI.txt" and store it in an object called "NITRATI":
read.table("NITRATI.txt", header = TRUE, sep = "\t")
Save the history and save the workspace:
savehistory(file = "day1.Rhistory")
save.image(file = "day1.RData")