Calculate the absolute, relative, relative percentage, and cumulative relative percentage frequency distribution of the variables "Peso" (Weight) and "Sesso" (Gender) contained in the file "dati_ottobre.txt".
Additionally, divide the variable "Altezza" (Height) into 3 classes.
SOLUTION:
Change the working directory
setwd("insert the path of the working folder")
Import the database "dati_ottobre.txt" using the read.table function.
Specify sep= "\t" to indicate that the different fields are separated by tabs, and header="TRUE" indicates that the first row of the file contains the variable names.
The argument dec="." specifies the character used in the file to separate decimals, in this case, a period.
The argument na.strings="NA" is useful if there are missing values in the file; in this case, it specifies that missing data is indicated by NA.
dati = read.table("dati_ottobre.txt", header = TRUE, sep = "\t", dec = ".", na.string = "NA")
To view the data matrix
View(dati)
To inspect the structure of the database
str(dati)
The attach function is important to make the variables directly accessible. If you don't attach the data.frame, to call a variable, you need to prefix it with dati$variable_name.
The attach command remains active for the entire work session unless its reverse, detach(dati), is executed.
attach(dati)
The dim function returns the number of rows and columns
dim(dati)
To get the names of the variables
colnames(dati)
To view the first rows of the data.frame
head(dati)
Absolute, relative, relative percentage, and cumulative relative percentage frequency distribution of the variable "Peso"
table(Peso)
table(Peso)/length(Peso)
table(Peso)/length(Peso)*100
cumsum(table(Peso)/length(Peso)*100)
Absolute, relative, relative percentage, and cumulative relative percentage frequency distribution of the variable "Sesso"
table(Sesso)
table(Sesso)/length(Sesso)
table(Sesso)/length(Sesso)*100
cumsum(table(Sesso)/length(Sesso)*100)
Divide the variable "Altezza" into 3 classes
Altezza_classi <- cut(Altezza, breaks = 3)
Altezza_classi