#01 Check the working directory in R
getwd()
#02 Create a folder named "pluto" on the desktop of your computer
dir.create("/Desktop/pluto")
#03 Move the working directory to the folder created in the previous step
setwd("/Desktop/pluto")
#04 Check the packages and datasets loaded in the workspace. Is the "chickwts" dataset present?
chickwts
or
exists("chickwts")
#05 Install the "car" package using the command line and then load it
install.packages("car")
library(lmtest)
#06 Create a variable named "F" and assign it the value 30
F<-30
#07 Check the class of the variable F
class(F)
#08 Create a variable named "F2" and assign it the value "30" (with quotes)
F2<-"30"
#09 Check the class of the variable F2
class(F2)
#10 Create a variable named "G" and assign it the value "CASO"
G<-"CASO"
#11 Create the variable "G2" by copying the variable G
G2<-G
#12 Replace the letter "O" with the letter "A" in the value of the variable G2
sub("O","A",G2)
#13 Delete the variables F, F2, G, and G2
rm(F,F2,G,G2)
#14 Calculate the area of a circle with a radius of 5cm
5^2*pi
15 Calculate the area of a trapezoid given that the major base, minor base, and height measure 9cm, 3cm, and 2cm respectively
(9+3)*2/2
#16 Create a vector that contains the values 1,22,3,44,56 using the scan function (press "enter" twice when finished entering values)
scan()
#17 Create a vector that contains the values 1,22,3,44,56 using the "c" operator
vect<-c(1,22,3,44,56)
#18 Create a vector that contains the values 101,102,103,...,999,1000
#Do not use the seq function
v<-101:1000
#19 Create a vector that contains all the even numbers from 102 to 1000
seq(102,1000,2)
#20 Create a vector that contains the values "I", "II", "III", "IV", and "V"
°Each value should be repeated 10 times before moving on to the next value
rep(c("I","II","III","IV","V"),each=10)
#21 Create a vector that contains the values "I", "II", "III", "IV", and "V"and make the sequence of letters repeat 10 times
rep(c("I","II","III","IV","V"),10)
#22 Create a logical vector of 40 elements that contains 10 F and 30 T
vect<-c(rep(F,30),rep(T,20))
#23 Create a dataframe named "precipitazioni"
The dataframe should contain a column named "citta" and another named "NO3".
The column named "citta" should contain the values
"Genova", "Milano", "Perugia", "Roma", "Avellino", "Lecce", "Cagliari", "Palermo";
the column named "mm" should contain the values
7.0, 10.3, 6.3, 7.2, 6.2, 5.4, 4.2, 4.1
Display the dataframe
citta<-c("Genova","Milano","Perugia","Roma","Avellino","Lecce","Cagliari","Palermo")
millimetri<-c(7.0, 10.3, 6.3, 7.2, 6.2, 5.4, 4.2, 4.1)
precipitazioni<-data.frame("citta"=citta,"mm"=millimetri)
precipitazioni
#24 Display the cities
precipitazioni$citta
#25 Extract the cities and display them in alphabetical order
sort(precipitazioni$citta)
#26 Extract the mm value for the city of Cagliari
subset(precipitazioni, citta=="Cagliari")[,2] precipitazioni$mm[precipitazioni$citta=="Cagliari"]
#27 Sum the mm values
sum(precipitazioni$mm)
#28 Calculate the maximum, minimum, mean, and median of mm
summary(precipitazioni$mm)
#29 Display the cities with mm<6
subset(precipitazioni, mm<6)[,1]
precipitazioni$citta[precipitazioni$mm<6]
#30 Rename the columns of the dataframe by converting the column names to uppercase
colnames(precipitazioni)<-toupper(colnames(precipitazioni))
#31 Convert the columns back to lowercase
colnames(precipitazioni)<-tolower(colnames(precipitazioni))
#32 Add an additional column to the dataframe, name it "posizione" and insert the values "nord", "nord", "centro", "centro", "sud", "sud", "isola", "isola"
precipitazioni$posizione<-c("nord", "nord", "centro", "centro", "sud", "sud", "isola", "isola")
#33 Extract all rows where posizione="sud" and save them in a new dataframe
df<-subset(precipitazioni,posizione=="sud")
df<-precipitazioni[precipitazioni$posizione=="sud",]
#34 Create two vectors containing the numbers from 11 to 30 and the numbers from 21 to 40 respectively. Then use rnorm to create two vectors containing 20 pseudo-random values each from a standard normal distribution. Name the four vectors W, X, Y, and Z
W<-c(11:30)
X<-c(21:40)
Y<-rnorm(20)
Z<-rnorm(20)
#35 Display the contingency table of W and X
xtabs(~X+W)
#36 Display the common elements of vectors W and X
intersect(W,X)
#37 Display the elements of W that are not in X
subtract(W,X)
#38 Verify that the value 65 is not in X
which(X==65)
#39 Display the positions of elements greater than 28 in vector X (use which)
which(X>28)
#40 Indicate the position of the minimum value of Z
a<-min(Z) which(Z==a)
#41 Round all values of Y down first, then up
floor(Y)
ceiling(Y)
#42 Create a factor vector called "esito" containing 40 times the value "positivo" and 60 times the value "negativo"
esito<-c(rep("positivo",40),rep("negativo",60))
#43 Convert the values of "esito" from "positivo" and "negativo" to "+" and "-" respectively
esito<-as.factor(esito)
levels(esito)<-c("-","+")
#44 Create a 6x6 matrix with 36 random numbers
x<-rnorm(36)
dim(x)<-c(6,6)
#45 Extract the sixth row of the matrix and add 5 to the entire row
x[6,]+5
#46 Extract the fourth column of the matrix and subtract 1
x[,4]-1
#47 Halve all the values in the matrix
x/2
#48 Create the dataframe "prov" containing 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 the dataframes preciptazioni and prov with merge
#and create the new dataframe "DATI" #(only the rows where the cities match will be kept)
DATI<-merge(precipitazioni,prov,by.x="citta",by.y="citta",all=T)
na.omit(DATI)
#50 Use the paste0 function to create a new variable that contains the cities, a space, and in parentheses the province abbreviation. Example: Palermo (PA)
paste(prov$citta,"(",prov$provincia,")",sep=" ")
#51 Use the dataframe "mtcars", already present in R, and display the names of the variables
colnames(mtcars)
#52 Select the values of "carb" from mtcars where gear="1". Ensure the carb values appear only once
unique(subset(mtcars,gear==1)[,1])
#53 Select the rows from mtcars where cyl="6" and create a new dataframe
#including only the columns "mpg", "gear", and "carb
subset(mtcars,cyl==6)[,c(1,3,10)]
#54 Use the dataframe USJudgeRatings and calculate the median for each variable
#using the apply function
apply(USArrests,2,median)
#55 Save the dataframe "precipitazioni" in a text file named "PRECIPITAZIONI.txt"
write.table(precipitazioni,"PRECIPITAZIONI.txt",sep="\t")
#56 Load the file "PRECIPITAZIONI.txt" and store it in an object called "PRECIPITAZIONI"
PRECIPITAZIONI<-read.table("NITRATI.txt",header=TRUE, sep="\t")
#57 Save the history and save the workspace
savehistory(file = "giorno1.Rhistory");
save.image(file = "giorno1.RData");