Exercici 1:

library(MASS)
library(survival)

Exercici 2:

setwd("C:/Users/pbujosa/OneDrive - San Juan de Dios/Màster Bioinformàtica Bioestadística/S1_Programari per a l'anàlisi de dades/R environment/LAB1")
library(readr)

a)

Vaccination_data1 <- read.delim("vaccination-data.txt")
head(Vaccination_data1, n = 5)
summary(Vaccination_data1)
##    COUNTRY              ISO3            WHO_REGION        DATA_SOURCE       
##  Length:215         Length:215         Length:215         Length:215        
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##  DATE_UPDATED       TOTAL_VACCINATIONS  PERSONS_VACCINATED_1PLUS_DOSE
##  Length:215         Min.   :4.619e+03   Min.   :1.638e+03            
##  Class :character   1st Qu.:9.034e+05   1st Qu.:4.511e+05            
##  Mode  :character   Median :7.372e+06   Median :3.746e+06            
##                     Mean   :6.536e+07   Mean   :2.687e+07            
##                     3rd Qu.:2.730e+07   3rd Qu.:1.360e+07            
##                     Max.   :3.520e+09   Max.   :1.320e+09            
##                     NA's   :6           NA's   :6                    
##  TOTAL_VACCINATIONS_PER100 PERSONS_VACCINATED_1PLUS_DOSE_PER100
##  Min.   :  4.0             Min.   :  4.00                      
##  1st Qu.: 82.0             1st Qu.: 45.00                      
##  Median :154.0             Median : 67.00                      
##  Mean   :153.9             Mean   : 62.77                      
##  3rd Qu.:221.5             3rd Qu.: 83.00                      
##  Max.   :470.0             Max.   :100.00                      
##  NA's   :8                 NA's   :8                           
##  PERSONS_LAST_DOSE   PERSONS_LAST_DOSE_PER100 FIRST_VACCINE_DATE
##  Min.   :1.635e+03   Min.   :  3.00           Length:215        
##  1st Qu.:3.971e+05   1st Qu.: 38.50           Class :character  
##  Median :3.325e+06   Median : 62.00           Mode  :character  
##  Mean   :2.470e+07   Mean   : 57.76                             
##  3rd Qu.:1.110e+07   3rd Qu.: 77.50                             
##  Max.   :1.280e+09   Max.   :100.00                             
##  NA's   :6           NA's   :8                                  
##  PERSONS_BOOSTER_ADD_DOSE PERSONS_BOOSTER_ADD_DOSE_PER100
##  Min.   :      314        Min.   : 1.00                  
##  1st Qu.:    76120        1st Qu.:10.00                  
##  Median :   803560        Median :31.00                  
##  Mean   : 12856725        Mean   :33.12                  
##  3rd Qu.:  5230598        3rd Qu.:56.00                  
##  Max.   :834000000        Max.   :83.00                  
##  NA's   :20               NA's   :31

b)

Vaccination_data2 <- read.csv("vaccination-data.csv")
head(Vaccination_data2, n = 2)
fivenum(Vaccination_data2$PERSONS_VACCINATED_1PLUS_DOSE_PER100)
## [1]   4  45  67  83 100
fivenum(Vaccination_data2$PERSONS_LAST_DOSE_PER100)
## [1]   3.0  38.5  62.0  77.5 100.0
fivenum(Vaccination_data2$PERSONS_BOOSTER_ADD_DOSE_PER100)
## [1]  1 10 31 56 83

Exercici 3

head(anorexia, n = 3)
na_values <- is.na(anorexia)
# Ja que sum conta TRUE com 1 i FALSE com 0 el resultat
# serà el nombre de valors NA a anorexia

sum(na_values)
## [1] 0
null_values <- is.null(anorexia)
sum(null_values)
## [1] 0
# Veiem que no hi ha cap valor NA ni NULL a les nostres
# dades

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
## 
##     select
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Generem el nou vector amb els valors canviats
anorexia_NewTreatName <- factor(anorexia$Treat, levels = c("CBT",
    "Cont", "FT"), labels = c("Cogn Beh Tr", "Contr", "Fam Tr"))
# Intercanviem la primera columna per nou vector generat
anorexia[1] <- anorexia_NewTreatName

anorexia[1, ]
anorexia[27, ]
anorexia[56, ]

Exercici 4:

a)

write.csv(biopsy, file = "C:/Users/pbujosa/OneDrive - San Juan de Dios/Màster Bioinformàtica Bioestadística/S1_Programari per a l'anàlisi de dades/R environment/LAB1/MASS_biopsy.csv")

b)

write.csv(Melanoma, file = "C:/Users/pbujosa/OneDrive - San Juan de Dios/Màster Bioinformàtica Bioestadística/S1_Programari per a l'anàlisi de dades/R environment/LAB1/MASS_melanoma.csv")
write.table(Melanoma, "C:/Users/pbujosa/OneDrive - San Juan de Dios/Màster Bioinformàtica Bioestadística/S1_Programari per a l'anàlisi de dades/R environment/LAB1/MASS_melanoma.txt",
    sep = "")
library(xlsx)
## Warning: package 'xlsx' was built under R version 4.4.3
write.xlsx(Melanoma, "C:/Users/pbujosa/OneDrive - San Juan de Dios/Màster Bioinformàtica Bioestadística/S1_Programari per a l'anàlisi de dades/R environment/LAB1/MASS_melanoma.xlsx")

Els documents s’han generat:

c)

sum_age <- summary(Melanoma$age)
capture.output(sum_age, file = "Melanoma_age_sum.doc")

d)

Age_death_rate <- read.csv("ESP_m_full_idr.csv")
head(Age_death_rate, n = 4)

Exercici 5:

a)

max(birthwt$age)
## [1] 45

L’edat màxima és 45 anys.

b)

min(birthwt$age)
## [1] 14

L’edat mínima és 14 anys.

c)

max_edat <- max(birthwt$age)
min_edat <- min(birthwt$age)
max_edat - min_edat
## [1] 31

El rang d’edat és 31 anys.

d)

birthwt$smoke[birthwt$bwt == min(birthwt$bwt)]
## [1] 1

Sí, la mare del nounat amb menor pes era fumadora.

e)

birthwt$bwt[birthwt$age == max(birthwt$age)]
## [1] 4990

El nounat de la mare de més edat va pesar 4990 grams.

f)

Pesos_menys2visites <- birthwt$bwt[birthwt$ftv < 2]
head(Pesos_menys2visites)
## [1] 2523 2557 2600 2622 2637 2637

Exercici 6:

matrix_anorexia <- matrix(c(anorexia$Prewt, anorexia$Postwt),
    ncol = 2)
head(matrix_anorexia, n = 3)
##      [,1] [,2]
## [1,] 80.7 80.2
## [2,] 89.4 80.1
## [3,] 91.8 86.4

Exercici 7:

Identificador <- c("I1", "I2", "I3", "I4", "I5", "I6", "I7",
    "I8", "I9", "I10", "I11", "I12", "I13", "I14", "I15", "I16",
    "I17", "I18", "I19", "I20", "I21", "I22", "I23", "I24", "I25")
Edat <- c(23, 24, 21, 22, 23, 25, 26, 24, 21, 22, 23, 25, 26,
    24, 22, 21, 25, 26, 24, 21, 25, 27, 26, 22, 29)
Sexe <- c(1, 2, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 1,
    2, 2, 2, 1, 2, 1, 1, 2)  #1 per a dones i 2 per a homes
Pes <- c(76.5, 81.2, 79.3, 59.5, 67.3, 78.6, 67.9, 100.2, 97.8,
    56.4, 65.4, 67.5, 87.4, 99.7, 87.6, 93.4, 65.4, 73.7, 85.1,
    61.2, 54.8, 103.4, 65.8, 71.7, 85)
Alt <- c(165, 154, 178, 165, 164, 175, 182, 165, 178, 165, 158,
    183, 184, 164, 189, 167, 182, 179, 165, 158, 183, 184, 189,
    166, 175)  #altura en cm
Fuma <- c("SÍ", "NO", "SÍ", "SÍ", "NO", "NO", "NO", "SÍ",
    "SÍ", "SÍ", "NO", "NO", "SÍ", "SÍ", "SÍ", "SÍ", "NO",
    "NO", "SÍ", "SÍ", "SÍ", "NO", "SÍ", "NO", "SÍ")
Tract_Pulmo <- data.frame(Identificador, Edat, Sexe, Pes, Alt,
    Fuma)
head(Tract_Pulmo)

a)

subset(Tract_Pulmo, Edat > 22)

b)

Tract_Pulmo[3, 4]
## [1] 79.3

c)

Tract_Pulmo_menors27 <- subset(Tract_Pulmo, Edat < 27, select = -c(Alt))
head(Tract_Pulmo_menors27)

Exercici 8:

a)

data(ChickWeight)

b)

plot(ChickWeight$weight, main = "Weight", col = "blue")

c)

boxplot(ChickWeight$Time, horizontal = TRUE, col = "orange2",
    main = "Time")

Exercici 9:

data(anorexia)
dif_weight <- c(anorexia$Prewt - anorexia$Postwt)
Treat <- c(anorexia$Treat)
anorexia_treat_df <- data.frame(Treat, dif_weight)
head(anorexia_treat_df, n = 3)
# Per selecionar les persones que han guanyat pes hem de
# seleccionar que la diferència entre el pes inicial i el
# pes final sigui menor que zero
anorexia_treat_C_df <- subset(anorexia_treat_df, Treat == "Cont" &
    dif_weight < 0)
head(anorexia_treat_C_df, n = 3)