R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

num1 = 10 num2 = 23 num1 + num2

#exemple calcul IMC = pes/alçada^2 pes = 57 alçada = 1.69 IMC = pes/(alçada)^2 IMC num1==num2 num1!=num2

#vectors vec <- c(1.5, 2, 3.5, 4) vec is.vector(vec)

#exemple MASS library (MASS) #carreguem el paquet MASS que tenim prèviament instal·lat
data(“birthwt”)#activem les dades
View(birthwt) #mostra el conjunt de dades en format taula
dim (birthwt) #mostra el nombre d’observacions i el nombre de variables

library(MASS)
## Warning: package 'MASS' was built under R version 4.5.3
data("birthwt")
View(birthwt)
dim(birthwt)
## [1] 189  10

length(birthwt) head(birthwt, n=5) names(birthwt)

#Midataf

genere <- c(1, 2, 1, 1, 1, 2, 2, 2, 1, 2)   
edat <- c(24, 25, 26, 24, 25, 27, 21, 22, 25, 26)   
fuma <- c("no", "sí", "no", "sí", "no", "no", "sí", "no", "no", "sí")
Midata <- data.frame(genere, edat, fuma)
Midata
##    genere edat fuma
## 1       1   24   no
## 2       2   25   sí
## 3       1   26   no
## 4       1   24   sí
## 5       1   25   no
## 6       2   27   no
## 7       2   21   sí
## 8       2   22   no
## 9       1   25   no
## 10      2   26   sí
Midata[[2]][3:5]
## [1] 26 24 25
Midata$genere
##  [1] 1 2 1 1 1 2 2 2 1 2
mean(Midata$edat[Midata$fuma=="no"])
## [1] 24.83333
mean(Midata$edat[Midata$fuma=="no"])
## [1] 24.83333
attach(Midata)
## The following objects are masked _by_ .GlobalEnv:
## 
##     edat, fuma, genere
table(fuma, genere)
##     genere
## fuma 1 2
##   no 4 2
##   sí 1 3
detach(Midata)

#exemple 8

Dataf_Enf1 = data.frame(malaltia = c("diabetis", "colesterol", "hipertensio", "hipotensio"), individus = c("ind1", "ind2", "ind3", "ind4"))
Dataf_Enf1
##      malaltia individus
## 1    diabetis      ind1
## 2  colesterol      ind2
## 3 hipertensio      ind3
## 4  hipotensio      ind4
Dataf_Enf2 = data.frame(malaltia = c("diabetis", "colesterol", "hipertensio", "hipoteniso"), individus = c("ind21", "ind22", "ind23", "ind24"))
Dataf_Enf2
##      malaltia individus
## 1    diabetis     ind21
## 2  colesterol     ind22
## 3 hipertensio     ind23
## 4  hipoteniso     ind24
#combinem els dos dataframes amb rbind()
#ja que tenen les mateixes variables
Data_Enf = rbind(Dataf_Enf1, Dataf_Enf2)
Data_Enf
##      malaltia individus
## 1    diabetis      ind1
## 2  colesterol      ind2
## 3 hipertensio      ind3
## 4  hipotensio      ind4
## 5    diabetis     ind21
## 6  colesterol     ind22
## 7 hipertensio     ind23
## 8  hipoteniso     ind24

#exemple 9

Indiv1 <- c("I213", "I214", "I215", "I216", "I217")
Medic1 <- c("Paracetamol", "Ibuprofen", "Aspirina", "Ibuprofen", "Paracetamol")
Past_dia <- c(2, 3, 2, 2, 2)
dfmedica1 <- data.frame(Indiv1, Medic1, Past_dia)
dfmedica1
##   Indiv1      Medic1 Past_dia
## 1   I213 Paracetamol        2
## 2   I214   Ibuprofen        3
## 3   I215    Aspirina        2
## 4   I216   Ibuprofen        2
## 5   I217 Paracetamol        2
Indiv2 <- c("I213", "I214", "I215", "I216", "I217")
Medic2 <- c("Paracetamol", "Ibuprofe", "Aspirina", "Ibuprofe", "Paracetamol")
Past_dia <- c(2, 3, 2, 2, 2)
dfmedica2 <- data.frame(Indiv2, Medic2, Past_dia)

dfmedica2
##   Indiv2      Medic2 Past_dia
## 1   I213 Paracetamol        2
## 2   I214    Ibuprofe        3
## 3   I215    Aspirina        2
## 4   I216    Ibuprofe        2
## 5   I217 Paracetamol        2
merge(dfmedica1, dfmedica2)
##    Past_dia Indiv1      Medic1 Indiv2      Medic2
## 1         2   I213 Paracetamol   I213 Paracetamol
## 2         2   I213 Paracetamol   I215    Aspirina
## 3         2   I213 Paracetamol   I216    Ibuprofe
## 4         2   I213 Paracetamol   I217 Paracetamol
## 5         2   I215    Aspirina   I213 Paracetamol
## 6         2   I215    Aspirina   I215    Aspirina
## 7         2   I215    Aspirina   I216    Ibuprofe
## 8         2   I215    Aspirina   I217 Paracetamol
## 9         2   I216   Ibuprofen   I213 Paracetamol
## 10        2   I216   Ibuprofen   I215    Aspirina
## 11        2   I216   Ibuprofen   I216    Ibuprofe
## 12        2   I216   Ibuprofen   I217 Paracetamol
## 13        2   I217 Paracetamol   I213 Paracetamol
## 14        2   I217 Paracetamol   I215    Aspirina
## 15        2   I217 Paracetamol   I216    Ibuprofe
## 16        2   I217 Paracetamol   I217 Paracetamol
## 17        3   I214   Ibuprofen   I214    Ibuprofe

#exemple 10

set.seed(999)
metge.id <- 1:10
metge.nom <- c("Ona", "Jordi", "Oriol", "Pau", "Ester", "Xavi", "Jan", "Marta", "Anna", "Abril") #variables amb el nom de cada metge
metge_sal <- round(rnorm(10, mean = 1500, sd = 200)) #salari estimat aleatori de cada un
metge_edat <- round(rnorm(10, mean = 50, sd = 8))
metge_espec <- c("Neuro", "Orto", "Gine", "Trauma", rep("General", 6))

df_Med_1 <- data.frame(id = metge.id[1:8], nom = metge.nom [1:8], salari = metge_sal[1:8])
df_Med_2 <- data.frame(id = metge.id[-5], nom = metge.nom[-5], edat = metge_edat[-5], posicio = metge_espec[-5])

df_Med_1
##   id   nom salari
## 1  1   Ona   1444
## 2  2 Jordi   1237
## 3  3 Oriol   1659
## 4  4   Pau   1554
## 5  5 Ester   1445
## 6  6  Xavi   1387
## 7  7   Jan   1124
## 8  8 Marta   1247
df_Med_2
##   id   nom edat posicio
## 1  1   Ona   61   Neuro
## 2  2 Jordi   51    Orto
## 3  3 Oriol   58    Gine
## 4  4   Pau   51  Trauma
## 5  6  Xavi   39 General
## 6  7   Jan   51 General
## 7  8 Marta   51 General
## 8  9  Anna   57 General
## 9 10 Abril   33 General
merge(df_Med_1, df_Med_2)
##   id   nom salari edat posicio
## 1  1   Ona   1444   61   Neuro
## 2  2 Jordi   1237   51    Orto
## 3  3 Oriol   1659   58    Gine
## 4  4   Pau   1554   51  Trauma
## 5  6  Xavi   1387   39 General
## 6  7   Jan   1124   51 General
## 7  8 Marta   1247   51 General
merge(x=df_Med_1, y=df_Med_2, all = TRUE)
##    id   nom salari edat posicio
## 1   1   Ona   1444   61   Neuro
## 2   2 Jordi   1237   51    Orto
## 3   3 Oriol   1659   58    Gine
## 4   4   Pau   1554   51  Trauma
## 5   5 Ester   1445   NA    <NA>
## 6   6  Xavi   1387   39 General
## 7   7   Jan   1124   51 General
## 8   8 Marta   1247   51 General
## 9   9  Anna     NA   57 General
## 10 10 Abril     NA   33 General
merge (x=df_Med_1, y=df_Med_2, all = TRUE)  
##    id   nom salari edat posicio
## 1   1   Ona   1444   61   Neuro
## 2   2 Jordi   1237   51    Orto
## 3   3 Oriol   1659   58    Gine
## 4   4   Pau   1554   51  Trauma
## 5   5 Ester   1445   NA    <NA>
## 6   6  Xavi   1387   39 General
## 7   7   Jan   1124   51 General
## 8   8 Marta   1247   51 General
## 9   9  Anna     NA   57 General
## 10 10 Abril     NA   33 General
merge(df_Med_1, df_Med_2)
##   id   nom salari edat posicio
## 1  1   Ona   1444   61   Neuro
## 2  2 Jordi   1237   51    Orto
## 3  3 Oriol   1659   58    Gine
## 4  4   Pau   1554   51  Trauma
## 5  6  Xavi   1387   39 General
## 6  7   Jan   1124   51 General
## 7  8 Marta   1247   51 General
merge(x=df_Med_1, y=df_Med_2, all = TRUE)
##    id   nom salari edat posicio
## 1   1   Ona   1444   61   Neuro
## 2   2 Jordi   1237   51    Orto
## 3   3 Oriol   1659   58    Gine
## 4   4   Pau   1554   51  Trauma
## 5   5 Ester   1445   NA    <NA>
## 6   6  Xavi   1387   39 General
## 7   7   Jan   1124   51 General
## 8   8 Marta   1247   51 General
## 9   9  Anna     NA   57 General
## 10 10 Abril     NA   33 General
merge(df_Med_1, df_Med_2)
##   id   nom salari edat posicio
## 1  1   Ona   1444   61   Neuro
## 2  2 Jordi   1237   51    Orto
## 3  3 Oriol   1659   58    Gine
## 4  4   Pau   1554   51  Trauma
## 5  6  Xavi   1387   39 General
## 6  7   Jan   1124   51 General
## 7  8 Marta   1247   51 General
merge(x=df_Med_1, y=df_Med_2, all = TRUE)
##    id   nom salari edat posicio
## 1   1   Ona   1444   61   Neuro
## 2   2 Jordi   1237   51    Orto
## 3   3 Oriol   1659   58    Gine
## 4   4   Pau   1554   51  Trauma
## 5   5 Ester   1445   NA    <NA>
## 6   6  Xavi   1387   39 General
## 7   7   Jan   1124   51 General
## 8   8 Marta   1247   51 General
## 9   9  Anna     NA   57 General
## 10 10 Abril     NA   33 General

#Seleccionar i filtrar registres

Id<-
c("I1","I2","I3","I4","I5","I6","I7","I8","I9","I10","I11","I12","I13","I14",
"I15","I16","I17","I18","I19","I20","I21","I22")    
Edat <- c(23,24,21,22,23,25,26,24,21,22,23,25,26,24,22,21,25,26,24,21,25,27)    
Sexe <-c(1,2,1,1,1,2,2,2,1,2,1,2,2,2,1,1,1,2,2,2,1,2)   
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)  
Altura <- 
c(165,154,178,165,164,175,182,165,178,165,158,183,184,164,189,167,182,179,165
,158,183,184)   
Pacients <- data.frame (Id, Edat, Sexe, Pes, Altura)    
#seleccionar un nou conjunt de dades anomenat Prova1 amb nomes les variables Id, Edat i sexe

Prova1 <- subset(Pacients, select = c(Id, Edat, Sexe))
Prova1
##     Id Edat Sexe
## 1   I1   23    1
## 2   I2   24    2
## 3   I3   21    1
## 4   I4   22    1
## 5   I5   23    1
## 6   I6   25    2
## 7   I7   26    2
## 8   I8   24    2
## 9   I9   21    1
## 10 I10   22    2
## 11 I11   23    1
## 12 I12   25    2
## 13 I13   26    2
## 14 I14   24    2
## 15 I15   22    1
## 16 I16   21    1
## 17 I17   25    1
## 18 I18   26    2
## 19 I19   24    2
## 20 I20   21    2
## 21 I21   25    1
## 22 I22   27    2
#seleccionar totes les diles que tenen una edat mes gran o igual que 24 anys en un conjunt anomenat prova2

Prova2 <- subset(Pacients, Edat >= 24)
Prova2
##     Id Edat Sexe   Pes Altura
## 2   I2   24    2  81.2    154
## 6   I6   25    2  78.6    175
## 7   I7   26    2  67.9    182
## 8   I8   24    2 100.2    165
## 12 I12   25    2  67.5    183
## 13 I13   26    2  87.4    184
## 14 I14   24    2  99.7    164
## 17 I17   25    1  65.4    182
## 18 I18   26    2  73.7    179
## 19 I19   24    2  85.1    165
## 21 I21   25    1  54.8    183
## 22 I22   27    2 103.4    184
#prova3 seleccionar totes les files que tenen una edat mes petita a 25 i no volem incloure la columna sexe

Prova3 <- subset(Pacients, Edat < 25, select = -c(Sexe))
Prova3
##     Id Edat   Pes Altura
## 1   I1   23  76.5    165
## 2   I2   24  81.2    154
## 3   I3   21  79.3    178
## 4   I4   22  59.5    165
## 5   I5   23  67.3    164
## 8   I8   24 100.2    165
## 9   I9   21  97.8    178
## 10 I10   22  56.4    165
## 11 I11   23  65.4    158
## 14 I14   24  99.7    164
## 15 I15   22  87.6    189
## 16 I16   21  93.4    167
## 19 I19   24  85.1    165
## 20 I20   21  61.2    158
#Prova 4 nomes els registres que tenen una alçada mes petita o igual que 165 o mes gran que 175cm

Prova4 <- subset(Pacients,Altura <= 165 | Altura > 175)
#exemple 12
set.seed(999)
f <- nrow(Pacients)
n <- 3
i <- sample(1:f, n, replace = FALSE)
Prova5 <- Pacients[i,]
Prova5
##   Id Edat Sexe  Pes Altura
## 4 I4   22    1 59.5    165
## 7 I7   26    2 67.9    182
## 9 I9   21    1 97.8    178
library(dplyr)
## 
## Adjuntando el paquete: '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
data("women")
Prova6 <- filter(women, height > 58)
Prova6
##    height weight
## 1      59    117
## 2      60    120
## 3      61    123
## 4      62    126
## 5      63    129
## 6      64    132
## 7      65    135
## 8      66    139
## 9      67    142
## 10     68    146
## 11     69    150
## 12     70    154
## 13     71    159
## 14     72    164
#Visualització de Gràfics
plot(iris)

hist(iris$Sepal.Length)

hist(iris$Sepal.Length, breaks = c(4,5,6,7,8), main = "Histograma", xlab= "cm", 
     ylab= "Freq", xlim = c(2,10), ylim = c(0,60), col = "blue")

boxplot(iris)

boxplot(iris[ ,-5], main = "Diagrames de caixa", xlab= "Dimensions", ylab = "cm", col = c("red", "blue", "yellow", "orange"))

#exercicis

#ex1
sessionInfo()
## R version 4.5.0 (2025-04-11 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 11 x64 (build 26200)
## 
## Matrix products: default
##   LAPACK version 3.12.1
## 
## locale:
## [1] LC_COLLATE=Spanish_Spain.utf8  LC_CTYPE=Spanish_Spain.utf8   
## [3] LC_MONETARY=Spanish_Spain.utf8 LC_NUMERIC=C                  
## [5] LC_TIME=Spanish_Spain.utf8    
## 
## time zone: Europe/Madrid
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] dplyr_1.1.4 MASS_7.3-66
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.37     R6_2.6.1          fastmap_1.2.0     tidyselect_1.2.1 
##  [5] xfun_0.52         magrittr_2.0.3    glue_1.8.0        cachem_1.1.0     
##  [9] tibble_3.2.1      knitr_1.52        pkgconfig_2.0.3   htmltools_0.5.8.1
## [13] generics_0.1.4    rmarkdown_2.29    lifecycle_1.0.5   cli_3.6.5        
## [17] vctrs_0.6.5       sass_0.4.10       jquerylib_0.1.4   compiler_4.5.0   
## [21] tools_4.5.0       pillar_1.10.2     evaluate_1.0.3    bslib_0.9.0      
## [25] yaml_2.3.10       rlang_1.1.6       jsonlite_2.0.0
library("MASS")
library("survival")
## Warning: package 'survival' was built under R version 4.5.3
sessionInfo()
## R version 4.5.0 (2025-04-11 ucrt)
## Platform: x86_64-w64-mingw32/x64
## Running under: Windows 11 x64 (build 26200)
## 
## Matrix products: default
##   LAPACK version 3.12.1
## 
## locale:
## [1] LC_COLLATE=Spanish_Spain.utf8  LC_CTYPE=Spanish_Spain.utf8   
## [3] LC_MONETARY=Spanish_Spain.utf8 LC_NUMERIC=C                  
## [5] LC_TIME=Spanish_Spain.utf8    
## 
## time zone: Europe/Madrid
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] survival_3.8-12 dplyr_1.1.4     MASS_7.3-66    
## 
## loaded via a namespace (and not attached):
##  [1] vctrs_0.6.5       cli_3.6.5         knitr_1.52        rlang_1.1.6      
##  [5] xfun_0.52         generics_0.1.4    jsonlite_2.0.0    glue_1.8.0       
##  [9] htmltools_0.5.8.1 sass_0.4.10       rmarkdown_2.29    grid_4.5.0       
## [13] evaluate_1.0.3    jquerylib_0.1.4   tibble_3.2.1      fastmap_1.2.0    
## [17] yaml_2.3.10       lifecycle_1.0.5   compiler_4.5.0    pkgconfig_2.0.3  
## [21] lattice_0.22-6    digest_0.6.37     R6_2.6.1          tidyselect_1.2.1 
## [25] pillar_1.10.2     splines_4.5.0     magrittr_2.0.3    Matrix_1.7-3     
## [29] bslib_0.9.0       tools_4.5.0       cachem_1.1.0
#exercici2
setwd("C:/Users/nl172/Downloads")
arxiu1 <- read.table("pacients_exercici.txt", header = TRUE, sep = "\t")

taula1 <- read.table("pacients_exercici.txt", header = TRUE, sep = "\t")
summary(taula1$Edat)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   21.00   23.75   26.50   26.75   29.25   35.00
taula2 <- read.table("estudi_clinic_exercici.csv", header = TRUE, sep = ",")
fivenum(taula2$Colesterol)
## [1] 175 189 205 220 242
#exercici3
library("MASS")
data("anorexia")
View(anorexia)
is.na(anorexia)
##    Treat Prewt Postwt
## 1  FALSE FALSE  FALSE
## 2  FALSE FALSE  FALSE
## 3  FALSE FALSE  FALSE
## 4  FALSE FALSE  FALSE
## 5  FALSE FALSE  FALSE
## 6  FALSE FALSE  FALSE
## 7  FALSE FALSE  FALSE
## 8  FALSE FALSE  FALSE
## 9  FALSE FALSE  FALSE
## 10 FALSE FALSE  FALSE
## 11 FALSE FALSE  FALSE
## 12 FALSE FALSE  FALSE
## 13 FALSE FALSE  FALSE
## 14 FALSE FALSE  FALSE
## 15 FALSE FALSE  FALSE
## 16 FALSE FALSE  FALSE
## 17 FALSE FALSE  FALSE
## 18 FALSE FALSE  FALSE
## 19 FALSE FALSE  FALSE
## 20 FALSE FALSE  FALSE
## 21 FALSE FALSE  FALSE
## 22 FALSE FALSE  FALSE
## 23 FALSE FALSE  FALSE
## 24 FALSE FALSE  FALSE
## 25 FALSE FALSE  FALSE
## 26 FALSE FALSE  FALSE
## 27 FALSE FALSE  FALSE
## 28 FALSE FALSE  FALSE
## 29 FALSE FALSE  FALSE
## 30 FALSE FALSE  FALSE
## 31 FALSE FALSE  FALSE
## 32 FALSE FALSE  FALSE
## 33 FALSE FALSE  FALSE
## 34 FALSE FALSE  FALSE
## 35 FALSE FALSE  FALSE
## 36 FALSE FALSE  FALSE
## 37 FALSE FALSE  FALSE
## 38 FALSE FALSE  FALSE
## 39 FALSE FALSE  FALSE
## 40 FALSE FALSE  FALSE
## 41 FALSE FALSE  FALSE
## 42 FALSE FALSE  FALSE
## 43 FALSE FALSE  FALSE
## 44 FALSE FALSE  FALSE
## 45 FALSE FALSE  FALSE
## 46 FALSE FALSE  FALSE
## 47 FALSE FALSE  FALSE
## 48 FALSE FALSE  FALSE
## 49 FALSE FALSE  FALSE
## 50 FALSE FALSE  FALSE
## 51 FALSE FALSE  FALSE
## 52 FALSE FALSE  FALSE
## 53 FALSE FALSE  FALSE
## 54 FALSE FALSE  FALSE
## 55 FALSE FALSE  FALSE
## 56 FALSE FALSE  FALSE
## 57 FALSE FALSE  FALSE
## 58 FALSE FALSE  FALSE
## 59 FALSE FALSE  FALSE
## 60 FALSE FALSE  FALSE
## 61 FALSE FALSE  FALSE
## 62 FALSE FALSE  FALSE
## 63 FALSE FALSE  FALSE
## 64 FALSE FALSE  FALSE
## 65 FALSE FALSE  FALSE
## 66 FALSE FALSE  FALSE
## 67 FALSE FALSE  FALSE
## 68 FALSE FALSE  FALSE
## 69 FALSE FALSE  FALSE
## 70 FALSE FALSE  FALSE
## 71 FALSE FALSE  FALSE
## 72 FALSE FALSE  FALSE
any(is.na(anorexia))
## [1] FALSE
library ("MASS")
data("anorexia")
anorexia$Treat <- as.character(anorexia$Treat)
anorexia$Treat[anorexia$Treat == "FT"] <- "ft"
anorexia$Treat <- factor(anorexia$Treat)
anorexia
##    Treat Prewt Postwt
## 1   Cont  80.7   80.2
## 2   Cont  89.4   80.1
## 3   Cont  91.8   86.4
## 4   Cont  74.0   86.3
## 5   Cont  78.1   76.1
## 6   Cont  88.3   78.1
## 7   Cont  87.3   75.1
## 8   Cont  75.1   86.7
## 9   Cont  80.6   73.5
## 10  Cont  78.4   84.6
## 11  Cont  77.6   77.4
## 12  Cont  88.7   79.5
## 13  Cont  81.3   89.6
## 14  Cont  78.1   81.4
## 15  Cont  70.5   81.8
## 16  Cont  77.3   77.3
## 17  Cont  85.2   84.2
## 18  Cont  86.0   75.4
## 19  Cont  84.1   79.5
## 20  Cont  79.7   73.0
## 21  Cont  85.5   88.3
## 22  Cont  84.4   84.7
## 23  Cont  79.6   81.4
## 24  Cont  77.5   81.2
## 25  Cont  72.3   88.2
## 26  Cont  89.0   78.8
## 27   CBT  80.5   82.2
## 28   CBT  84.9   85.6
## 29   CBT  81.5   81.4
## 30   CBT  82.6   81.9
## 31   CBT  79.9   76.4
## 32   CBT  88.7  103.6
## 33   CBT  94.9   98.4
## 34   CBT  76.3   93.4
## 35   CBT  81.0   73.4
## 36   CBT  80.5   82.1
## 37   CBT  85.0   96.7
## 38   CBT  89.2   95.3
## 39   CBT  81.3   82.4
## 40   CBT  76.5   72.5
## 41   CBT  70.0   90.9
## 42   CBT  80.4   71.3
## 43   CBT  83.3   85.4
## 44   CBT  83.0   81.6
## 45   CBT  87.7   89.1
## 46   CBT  84.2   83.9
## 47   CBT  86.4   82.7
## 48   CBT  76.5   75.7
## 49   CBT  80.2   82.6
## 50   CBT  87.8  100.4
## 51   CBT  83.3   85.2
## 52   CBT  79.7   83.6
## 53   CBT  84.5   84.6
## 54   CBT  80.8   96.2
## 55   CBT  87.4   86.7
## 56    ft  83.8   95.2
## 57    ft  83.3   94.3
## 58    ft  86.0   91.5
## 59    ft  82.5   91.9
## 60    ft  86.7  100.3
## 61    ft  79.6   76.7
## 62    ft  76.9   76.8
## 63    ft  94.2  101.6
## 64    ft  73.4   94.9
## 65    ft  80.5   75.2
## 66    ft  81.6   77.8
## 67    ft  82.1   95.5
## 68    ft  77.6   90.7
## 69    ft  83.5   92.5
## 70    ft  89.9   93.8
## 71    ft  86.0   91.7
## 72    ft  87.3   98.0
anorexia$Treat <- as.character(anorexia$Treat)
anorexia$Treat[anorexia$Treat == "Cont"] <- "Contr"
anorexia$Treat <- as.factor(anorexia$Treat)
anorexia
##    Treat Prewt Postwt
## 1  Contr  80.7   80.2
## 2  Contr  89.4   80.1
## 3  Contr  91.8   86.4
## 4  Contr  74.0   86.3
## 5  Contr  78.1   76.1
## 6  Contr  88.3   78.1
## 7  Contr  87.3   75.1
## 8  Contr  75.1   86.7
## 9  Contr  80.6   73.5
## 10 Contr  78.4   84.6
## 11 Contr  77.6   77.4
## 12 Contr  88.7   79.5
## 13 Contr  81.3   89.6
## 14 Contr  78.1   81.4
## 15 Contr  70.5   81.8
## 16 Contr  77.3   77.3
## 17 Contr  85.2   84.2
## 18 Contr  86.0   75.4
## 19 Contr  84.1   79.5
## 20 Contr  79.7   73.0
## 21 Contr  85.5   88.3
## 22 Contr  84.4   84.7
## 23 Contr  79.6   81.4
## 24 Contr  77.5   81.2
## 25 Contr  72.3   88.2
## 26 Contr  89.0   78.8
## 27   CBT  80.5   82.2
## 28   CBT  84.9   85.6
## 29   CBT  81.5   81.4
## 30   CBT  82.6   81.9
## 31   CBT  79.9   76.4
## 32   CBT  88.7  103.6
## 33   CBT  94.9   98.4
## 34   CBT  76.3   93.4
## 35   CBT  81.0   73.4
## 36   CBT  80.5   82.1
## 37   CBT  85.0   96.7
## 38   CBT  89.2   95.3
## 39   CBT  81.3   82.4
## 40   CBT  76.5   72.5
## 41   CBT  70.0   90.9
## 42   CBT  80.4   71.3
## 43   CBT  83.3   85.4
## 44   CBT  83.0   81.6
## 45   CBT  87.7   89.1
## 46   CBT  84.2   83.9
## 47   CBT  86.4   82.7
## 48   CBT  76.5   75.7
## 49   CBT  80.2   82.6
## 50   CBT  87.8  100.4
## 51   CBT  83.3   85.2
## 52   CBT  79.7   83.6
## 53   CBT  84.5   84.6
## 54   CBT  80.8   96.2
## 55   CBT  87.4   86.7
## 56    ft  83.8   95.2
## 57    ft  83.3   94.3
## 58    ft  86.0   91.5
## 59    ft  82.5   91.9
## 60    ft  86.7  100.3
## 61    ft  79.6   76.7
## 62    ft  76.9   76.8
## 63    ft  94.2  101.6
## 64    ft  73.4   94.9
## 65    ft  80.5   75.2
## 66    ft  81.6   77.8
## 67    ft  82.1   95.5
## 68    ft  77.6   90.7
## 69    ft  83.5   92.5
## 70    ft  89.9   93.8
## 71    ft  86.0   91.7
## 72    ft  87.3   98.0
anorexia$Treat <- as.character(anorexia$Treat)
anorexia$Treat[anorexia$Treat == "CBT"] <- "CbbT"
anorexia$Treat <- as.factor(anorexia$Treat)
anorexia
##    Treat Prewt Postwt
## 1  Contr  80.7   80.2
## 2  Contr  89.4   80.1
## 3  Contr  91.8   86.4
## 4  Contr  74.0   86.3
## 5  Contr  78.1   76.1
## 6  Contr  88.3   78.1
## 7  Contr  87.3   75.1
## 8  Contr  75.1   86.7
## 9  Contr  80.6   73.5
## 10 Contr  78.4   84.6
## 11 Contr  77.6   77.4
## 12 Contr  88.7   79.5
## 13 Contr  81.3   89.6
## 14 Contr  78.1   81.4
## 15 Contr  70.5   81.8
## 16 Contr  77.3   77.3
## 17 Contr  85.2   84.2
## 18 Contr  86.0   75.4
## 19 Contr  84.1   79.5
## 20 Contr  79.7   73.0
## 21 Contr  85.5   88.3
## 22 Contr  84.4   84.7
## 23 Contr  79.6   81.4
## 24 Contr  77.5   81.2
## 25 Contr  72.3   88.2
## 26 Contr  89.0   78.8
## 27  CbbT  80.5   82.2
## 28  CbbT  84.9   85.6
## 29  CbbT  81.5   81.4
## 30  CbbT  82.6   81.9
## 31  CbbT  79.9   76.4
## 32  CbbT  88.7  103.6
## 33  CbbT  94.9   98.4
## 34  CbbT  76.3   93.4
## 35  CbbT  81.0   73.4
## 36  CbbT  80.5   82.1
## 37  CbbT  85.0   96.7
## 38  CbbT  89.2   95.3
## 39  CbbT  81.3   82.4
## 40  CbbT  76.5   72.5
## 41  CbbT  70.0   90.9
## 42  CbbT  80.4   71.3
## 43  CbbT  83.3   85.4
## 44  CbbT  83.0   81.6
## 45  CbbT  87.7   89.1
## 46  CbbT  84.2   83.9
## 47  CbbT  86.4   82.7
## 48  CbbT  76.5   75.7
## 49  CbbT  80.2   82.6
## 50  CbbT  87.8  100.4
## 51  CbbT  83.3   85.2
## 52  CbbT  79.7   83.6
## 53  CbbT  84.5   84.6
## 54  CbbT  80.8   96.2
## 55  CbbT  87.4   86.7
## 56    ft  83.8   95.2
## 57    ft  83.3   94.3
## 58    ft  86.0   91.5
## 59    ft  82.5   91.9
## 60    ft  86.7  100.3
## 61    ft  79.6   76.7
## 62    ft  76.9   76.8
## 63    ft  94.2  101.6
## 64    ft  73.4   94.9
## 65    ft  80.5   75.2
## 66    ft  81.6   77.8
## 67    ft  82.1   95.5
## 68    ft  77.6   90.7
## 69    ft  83.5   92.5
## 70    ft  89.9   93.8
## 71    ft  86.0   91.7
## 72    ft  87.3   98.0
#exercici4
library(MASS)
data("biopsy")
write.csv("biopsy", "biopsy.csv")

data("Melanoma")
library("openxlsx")
## Warning: package 'openxlsx' was built under R version 4.5.3
write.csv(Melanoma, "melanoma.csv")
write.table(Melanoma, "melanoma2.txt")
write.xlsx(Melanoma, "melanoma2.xlsx")

data("Melanoma")
resumelanoma <- summary(Melanoma$age)
resum_df <- data.frame(Estadistic = names(resumelanoma),Valor = as.numeric(resumelanoma))
write.csv(resum_df, file = "resum_df.doc")
#exercici 5
library(MASS)
data("birthwt")
#edat maxima
max(birthwt$age)
## [1] 45
min(birthwt$age)
## [1] 14
range(birthwt$age)
## [1] 14 45
birthwt
##     low age lwt race smoke ptl ht ui ftv  bwt
## 85    0  19 182    2     0   0  0  1   0 2523
## 86    0  33 155    3     0   0  0  0   3 2551
## 87    0  20 105    1     1   0  0  0   1 2557
## 88    0  21 108    1     1   0  0  1   2 2594
## 89    0  18 107    1     1   0  0  1   0 2600
## 91    0  21 124    3     0   0  0  0   0 2622
## 92    0  22 118    1     0   0  0  0   1 2637
## 93    0  17 103    3     0   0  0  0   1 2637
## 94    0  29 123    1     1   0  0  0   1 2663
## 95    0  26 113    1     1   0  0  0   0 2665
## 96    0  19  95    3     0   0  0  0   0 2722
## 97    0  19 150    3     0   0  0  0   1 2733
## 98    0  22  95    3     0   0  1  0   0 2751
## 99    0  30 107    3     0   1  0  1   2 2750
## 100   0  18 100    1     1   0  0  0   0 2769
## 101   0  18 100    1     1   0  0  0   0 2769
## 102   0  15  98    2     0   0  0  0   0 2778
## 103   0  25 118    1     1   0  0  0   3 2782
## 104   0  20 120    3     0   0  0  1   0 2807
## 105   0  28 120    1     1   0  0  0   1 2821
## 106   0  32 121    3     0   0  0  0   2 2835
## 107   0  31 100    1     0   0  0  1   3 2835
## 108   0  36 202    1     0   0  0  0   1 2836
## 109   0  28 120    3     0   0  0  0   0 2863
## 111   0  25 120    3     0   0  0  1   2 2877
## 112   0  28 167    1     0   0  0  0   0 2877
## 113   0  17 122    1     1   0  0  0   0 2906
## 114   0  29 150    1     0   0  0  0   2 2920
## 115   0  26 168    2     1   0  0  0   0 2920
## 116   0  17 113    2     0   0  0  0   1 2920
## 117   0  17 113    2     0   0  0  0   1 2920
## 118   0  24  90    1     1   1  0  0   1 2948
## 119   0  35 121    2     1   1  0  0   1 2948
## 120   0  25 155    1     0   0  0  0   1 2977
## 121   0  25 125    2     0   0  0  0   0 2977
## 123   0  29 140    1     1   0  0  0   2 2977
## 124   0  19 138    1     1   0  0  0   2 2977
## 125   0  27 124    1     1   0  0  0   0 2922
## 126   0  31 215    1     1   0  0  0   2 3005
## 127   0  33 109    1     1   0  0  0   1 3033
## 128   0  21 185    2     1   0  0  0   2 3042
## 129   0  19 189    1     0   0  0  0   2 3062
## 130   0  23 130    2     0   0  0  0   1 3062
## 131   0  21 160    1     0   0  0  0   0 3062
## 132   0  18  90    1     1   0  0  1   0 3062
## 133   0  18  90    1     1   0  0  1   0 3062
## 134   0  32 132    1     0   0  0  0   4 3080
## 135   0  19 132    3     0   0  0  0   0 3090
## 136   0  24 115    1     0   0  0  0   2 3090
## 137   0  22  85    3     1   0  0  0   0 3090
## 138   0  22 120    1     0   0  1  0   1 3100
## 139   0  23 128    3     0   0  0  0   0 3104
## 140   0  22 130    1     1   0  0  0   0 3132
## 141   0  30  95    1     1   0  0  0   2 3147
## 142   0  19 115    3     0   0  0  0   0 3175
## 143   0  16 110    3     0   0  0  0   0 3175
## 144   0  21 110    3     1   0  0  1   0 3203
## 145   0  30 153    3     0   0  0  0   0 3203
## 146   0  20 103    3     0   0  0  0   0 3203
## 147   0  17 119    3     0   0  0  0   0 3225
## 148   0  17 119    3     0   0  0  0   0 3225
## 149   0  23 119    3     0   0  0  0   2 3232
## 150   0  24 110    3     0   0  0  0   0 3232
## 151   0  28 140    1     0   0  0  0   0 3234
## 154   0  26 133    3     1   2  0  0   0 3260
## 155   0  20 169    3     0   1  0  1   1 3274
## 156   0  24 115    3     0   0  0  0   2 3274
## 159   0  28 250    3     1   0  0  0   6 3303
## 160   0  20 141    1     0   2  0  1   1 3317
## 161   0  22 158    2     0   1  0  0   2 3317
## 162   0  22 112    1     1   2  0  0   0 3317
## 163   0  31 150    3     1   0  0  0   2 3321
## 164   0  23 115    3     1   0  0  0   1 3331
## 166   0  16 112    2     0   0  0  0   0 3374
## 167   0  16 135    1     1   0  0  0   0 3374
## 168   0  18 229    2     0   0  0  0   0 3402
## 169   0  25 140    1     0   0  0  0   1 3416
## 170   0  32 134    1     1   1  0  0   4 3430
## 172   0  20 121    2     1   0  0  0   0 3444
## 173   0  23 190    1     0   0  0  0   0 3459
## 174   0  22 131    1     0   0  0  0   1 3460
## 175   0  32 170    1     0   0  0  0   0 3473
## 176   0  30 110    3     0   0  0  0   0 3544
## 177   0  20 127    3     0   0  0  0   0 3487
## 179   0  23 123    3     0   0  0  0   0 3544
## 180   0  17 120    3     1   0  0  0   0 3572
## 181   0  19 105    3     0   0  0  0   0 3572
## 182   0  23 130    1     0   0  0  0   0 3586
## 183   0  36 175    1     0   0  0  0   0 3600
## 184   0  22 125    1     0   0  0  0   1 3614
## 185   0  24 133    1     0   0  0  0   0 3614
## 186   0  21 134    3     0   0  0  0   2 3629
## 187   0  19 235    1     1   0  1  0   0 3629
## 188   0  25  95    1     1   3  0  1   0 3637
## 189   0  16 135    1     1   0  0  0   0 3643
## 190   0  29 135    1     0   0  0  0   1 3651
## 191   0  29 154    1     0   0  0  0   1 3651
## 192   0  19 147    1     1   0  0  0   0 3651
## 193   0  19 147    1     1   0  0  0   0 3651
## 195   0  30 137    1     0   0  0  0   1 3699
## 196   0  24 110    1     0   0  0  0   1 3728
## 197   0  19 184    1     1   0  1  0   0 3756
## 199   0  24 110    3     0   1  0  0   0 3770
## 200   0  23 110    1     0   0  0  0   1 3770
## 201   0  20 120    3     0   0  0  0   0 3770
## 202   0  25 241    2     0   0  1  0   0 3790
## 203   0  30 112    1     0   0  0  0   1 3799
## 204   0  22 169    1     0   0  0  0   0 3827
## 205   0  18 120    1     1   0  0  0   2 3856
## 206   0  16 170    2     0   0  0  0   4 3860
## 207   0  32 186    1     0   0  0  0   2 3860
## 208   0  18 120    3     0   0  0  0   1 3884
## 209   0  29 130    1     1   0  0  0   2 3884
## 210   0  33 117    1     0   0  0  1   1 3912
## 211   0  20 170    1     1   0  0  0   0 3940
## 212   0  28 134    3     0   0  0  0   1 3941
## 213   0  14 135    1     0   0  0  0   0 3941
## 214   0  28 130    3     0   0  0  0   0 3969
## 215   0  25 120    1     0   0  0  0   2 3983
## 216   0  16  95    3     0   0  0  0   1 3997
## 217   0  20 158    1     0   0  0  0   1 3997
## 218   0  26 160    3     0   0  0  0   0 4054
## 219   0  21 115    1     0   0  0  0   1 4054
## 220   0  22 129    1     0   0  0  0   0 4111
## 221   0  25 130    1     0   0  0  0   2 4153
## 222   0  31 120    1     0   0  0  0   2 4167
## 223   0  35 170    1     0   1  0  0   1 4174
## 224   0  19 120    1     1   0  0  0   0 4238
## 225   0  24 116    1     0   0  0  0   1 4593
## 226   0  45 123    1     0   0  0  0   1 4990
## 4     1  28 120    3     1   1  0  1   0  709
## 10    1  29 130    1     0   0  0  1   2 1021
## 11    1  34 187    2     1   0  1  0   0 1135
## 13    1  25 105    3     0   1  1  0   0 1330
## 15    1  25  85    3     0   0  0  1   0 1474
## 16    1  27 150    3     0   0  0  0   0 1588
## 17    1  23  97    3     0   0  0  1   1 1588
## 18    1  24 128    2     0   1  0  0   1 1701
## 19    1  24 132    3     0   0  1  0   0 1729
## 20    1  21 165    1     1   0  1  0   1 1790
## 22    1  32 105    1     1   0  0  0   0 1818
## 23    1  19  91    1     1   2  0  1   0 1885
## 24    1  25 115    3     0   0  0  0   0 1893
## 25    1  16 130    3     0   0  0  0   1 1899
## 26    1  25  92    1     1   0  0  0   0 1928
## 27    1  20 150    1     1   0  0  0   2 1928
## 28    1  21 200    2     0   0  0  1   2 1928
## 29    1  24 155    1     1   1  0  0   0 1936
## 30    1  21 103    3     0   0  0  0   0 1970
## 31    1  20 125    3     0   0  0  1   0 2055
## 32    1  25  89    3     0   2  0  0   1 2055
## 33    1  19 102    1     0   0  0  0   2 2082
## 34    1  19 112    1     1   0  0  1   0 2084
## 35    1  26 117    1     1   1  0  0   0 2084
## 36    1  24 138    1     0   0  0  0   0 2100
## 37    1  17 130    3     1   1  0  1   0 2125
## 40    1  20 120    2     1   0  0  0   3 2126
## 42    1  22 130    1     1   1  0  1   1 2187
## 43    1  27 130    2     0   0  0  1   0 2187
## 44    1  20  80    3     1   0  0  1   0 2211
## 45    1  17 110    1     1   0  0  0   0 2225
## 46    1  25 105    3     0   1  0  0   1 2240
## 47    1  20 109    3     0   0  0  0   0 2240
## 49    1  18 148    3     0   0  0  0   0 2282
## 50    1  18 110    2     1   1  0  0   0 2296
## 51    1  20 121    1     1   1  0  1   0 2296
## 52    1  21 100    3     0   1  0  0   4 2301
## 54    1  26  96    3     0   0  0  0   0 2325
## 56    1  31 102    1     1   1  0  0   1 2353
## 57    1  15 110    1     0   0  0  0   0 2353
## 59    1  23 187    2     1   0  0  0   1 2367
## 60    1  20 122    2     1   0  0  0   0 2381
## 61    1  24 105    2     1   0  0  0   0 2381
## 62    1  15 115    3     0   0  0  1   0 2381
## 63    1  23 120    3     0   0  0  0   0 2410
## 65    1  30 142    1     1   1  0  0   0 2410
## 67    1  22 130    1     1   0  0  0   1 2410
## 68    1  17 120    1     1   0  0  0   3 2414
## 69    1  23 110    1     1   1  0  0   0 2424
## 71    1  17 120    2     0   0  0  0   2 2438
## 75    1  26 154    3     0   1  1  0   1 2442
## 76    1  20 105    3     0   0  0  0   3 2450
## 77    1  26 190    1     1   0  0  0   0 2466
## 78    1  14 101    3     1   1  0  0   0 2466
## 79    1  28  95    1     1   0  0  0   2 2466
## 81    1  14 100    3     0   0  0  0   2 2495
## 82    1  23  94    3     1   0  0  0   0 2495
## 83    1  17 142    2     0   0  1  0   0 2495
## 84    1  21 130    1     1   0  1  0   3 2495
min(birthwt$bwt)
## [1] 709
birthwt$smoke[birthwt$bwt == min(birthwt$bwt)]
## [1] 1
birthwt$bwt[birthwt$age == max(birthwt$age)]
## [1] 4990
subset(birthwt$bwt, birthwt$ftv < 2)
##   [1] 2523 2557 2600 2622 2637 2637 2663 2665 2722 2733 2751 2769 2769 2778 2807
##  [16] 2821 2836 2863 2877 2906 2920 2920 2920 2948 2948 2977 2977 2922 3033 3062
##  [31] 3062 3062 3062 3090 3090 3100 3104 3132 3175 3175 3203 3203 3203 3225 3225
##  [46] 3232 3234 3260 3274 3317 3317 3331 3374 3374 3402 3416 3444 3459 3460 3473
##  [61] 3544 3487 3544 3572 3572 3586 3600 3614 3614 3629 3637 3643 3651 3651 3651
##  [76] 3651 3699 3728 3756 3770 3770 3770 3790 3799 3827 3884 3912 3940 3941 3941
##  [91] 3969 3997 3997 4054 4054 4111 4174 4238 4593 4990  709 1135 1330 1474 1588
## [106] 1588 1701 1729 1790 1818 1885 1893 1899 1928 1936 1970 2055 2055 2084 2084
## [121] 2100 2125 2187 2187 2211 2225 2240 2240 2282 2296 2296 2325 2353 2353 2367
## [136] 2381 2381 2381 2410 2410 2410 2424 2442 2466 2466 2495 2495
#exerici 6
library(MASS)
data("anorexia")
matriu <- as.matrix(anorexia[, c("Prewt", "Postwt")])
matriu
##    Prewt Postwt
## 1   80.7   80.2
## 2   89.4   80.1
## 3   91.8   86.4
## 4   74.0   86.3
## 5   78.1   76.1
## 6   88.3   78.1
## 7   87.3   75.1
## 8   75.1   86.7
## 9   80.6   73.5
## 10  78.4   84.6
## 11  77.6   77.4
## 12  88.7   79.5
## 13  81.3   89.6
## 14  78.1   81.4
## 15  70.5   81.8
## 16  77.3   77.3
## 17  85.2   84.2
## 18  86.0   75.4
## 19  84.1   79.5
## 20  79.7   73.0
## 21  85.5   88.3
## 22  84.4   84.7
## 23  79.6   81.4
## 24  77.5   81.2
## 25  72.3   88.2
## 26  89.0   78.8
## 27  80.5   82.2
## 28  84.9   85.6
## 29  81.5   81.4
## 30  82.6   81.9
## 31  79.9   76.4
## 32  88.7  103.6
## 33  94.9   98.4
## 34  76.3   93.4
## 35  81.0   73.4
## 36  80.5   82.1
## 37  85.0   96.7
## 38  89.2   95.3
## 39  81.3   82.4
## 40  76.5   72.5
## 41  70.0   90.9
## 42  80.4   71.3
## 43  83.3   85.4
## 44  83.0   81.6
## 45  87.7   89.1
## 46  84.2   83.9
## 47  86.4   82.7
## 48  76.5   75.7
## 49  80.2   82.6
## 50  87.8  100.4
## 51  83.3   85.2
## 52  79.7   83.6
## 53  84.5   84.6
## 54  80.8   96.2
## 55  87.4   86.7
## 56  83.8   95.2
## 57  83.3   94.3
## 58  86.0   91.5
## 59  82.5   91.9
## 60  86.7  100.3
## 61  79.6   76.7
## 62  76.9   76.8
## 63  94.2  101.6
## 64  73.4   94.9
## 65  80.5   75.2
## 66  81.6   77.8
## 67  82.1   95.5
## 68  77.6   90.7
## 69  83.5   92.5
## 70  89.9   93.8
## 71  86.0   91.7
## 72  87.3   98.0
#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.0)
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)
Tract_Pulmo
##    Identificador Edat Sexe   Pes Alt Fuma
## 1             I1   23    1  76.5 165   SÍ
## 2             I2   24    2  81.2 154   NO
## 3             I3   21    1  79.3 178   SÍ
## 4             I4   22    1  59.5 165   SÍ
## 5             I5   23    1  67.3 164   NO
## 6             I6   25    2  78.6 175   NO
## 7             I7   26    2  67.9 182   NO
## 8             I8   24    2 100.2 165   SÍ
## 9             I9   21    1  97.8 178   SÍ
## 10           I10   22    2  56.4 165   SÍ
## 11           I11   23    1  65.4 158   NO
## 12           I12   25    2  67.5 183   NO
## 13           I13   26    2  87.4 184   SÍ
## 14           I14   24    2  99.7 164   SÍ
## 15           I15   22    1  87.6 189   SÍ
## 16           I16   21    1  93.4 167   SÍ
## 17           I17   25    1  65.4 182   NO
## 18           I18   26    2  73.7 179   NO
## 19           I19   24    2  85.1 165   SÍ
## 20           I20   21    2  61.2 158   SÍ
## 21           I21   25    1  54.8 183   SÍ
## 22           I22   27    2 103.4 184   NO
## 23           I23   26    1  65.8 189   SÍ
## 24           I24   22    1  71.7 166   NO
## 25           I25   29    2  85.0 175   SÍ
subset(Tract_Pulmo, Edat > 22)
##    Identificador Edat Sexe   Pes Alt Fuma
## 1             I1   23    1  76.5 165   SÍ
## 2             I2   24    2  81.2 154   NO
## 5             I5   23    1  67.3 164   NO
## 6             I6   25    2  78.6 175   NO
## 7             I7   26    2  67.9 182   NO
## 8             I8   24    2 100.2 165   SÍ
## 11           I11   23    1  65.4 158   NO
## 12           I12   25    2  67.5 183   NO
## 13           I13   26    2  87.4 184   SÍ
## 14           I14   24    2  99.7 164   SÍ
## 17           I17   25    1  65.4 182   NO
## 18           I18   26    2  73.7 179   NO
## 19           I19   24    2  85.1 165   SÍ
## 21           I21   25    1  54.8 183   SÍ
## 22           I22   27    2 103.4 184   NO
## 23           I23   26    1  65.8 189   SÍ
## 25           I25   29    2  85.0 175   SÍ
Tract_Pulmo[3, 4]
## [1] 79.3
subset(Tract_Pulmo, Edat < 27, -c(Alt))
##    Identificador Edat Sexe   Pes Fuma
## 1             I1   23    1  76.5   SÍ
## 2             I2   24    2  81.2   NO
## 3             I3   21    1  79.3   SÍ
## 4             I4   22    1  59.5   SÍ
## 5             I5   23    1  67.3   NO
## 6             I6   25    2  78.6   NO
## 7             I7   26    2  67.9   NO
## 8             I8   24    2 100.2   SÍ
## 9             I9   21    1  97.8   SÍ
## 10           I10   22    2  56.4   SÍ
## 11           I11   23    1  65.4   NO
## 12           I12   25    2  67.5   NO
## 13           I13   26    2  87.4   SÍ
## 14           I14   24    2  99.7   SÍ
## 15           I15   22    1  87.6   SÍ
## 16           I16   21    1  93.4   SÍ
## 17           I17   25    1  65.4   NO
## 18           I18   26    2  73.7   NO
## 19           I19   24    2  85.1   SÍ
## 20           I20   21    2  61.2   SÍ
## 21           I21   25    1  54.8   SÍ
## 23           I23   26    1  65.8   SÍ
## 24           I24   22    1  71.7   NO
#exercici 8
data("ChickWeight")
plot(ChickWeight$weight)

boxplot(ChickWeight$Time)

#exercici 9
library(MASS)
data("anorexia")
anorexia_treat_df <- data.frame(anorexia$Treat, c((anorexia$Postwt)-(anorexia$Prewt)))
anorexia_treat_df
##    anorexia.Treat c..anorexia.Postwt.....anorexia.Prewt..
## 1            Cont                                    -0.5
## 2            Cont                                    -9.3
## 3            Cont                                    -5.4
## 4            Cont                                    12.3
## 5            Cont                                    -2.0
## 6            Cont                                   -10.2
## 7            Cont                                   -12.2
## 8            Cont                                    11.6
## 9            Cont                                    -7.1
## 10           Cont                                     6.2
## 11           Cont                                    -0.2
## 12           Cont                                    -9.2
## 13           Cont                                     8.3
## 14           Cont                                     3.3
## 15           Cont                                    11.3
## 16           Cont                                     0.0
## 17           Cont                                    -1.0
## 18           Cont                                   -10.6
## 19           Cont                                    -4.6
## 20           Cont                                    -6.7
## 21           Cont                                     2.8
## 22           Cont                                     0.3
## 23           Cont                                     1.8
## 24           Cont                                     3.7
## 25           Cont                                    15.9
## 26           Cont                                   -10.2
## 27            CBT                                     1.7
## 28            CBT                                     0.7
## 29            CBT                                    -0.1
## 30            CBT                                    -0.7
## 31            CBT                                    -3.5
## 32            CBT                                    14.9
## 33            CBT                                     3.5
## 34            CBT                                    17.1
## 35            CBT                                    -7.6
## 36            CBT                                     1.6
## 37            CBT                                    11.7
## 38            CBT                                     6.1
## 39            CBT                                     1.1
## 40            CBT                                    -4.0
## 41            CBT                                    20.9
## 42            CBT                                    -9.1
## 43            CBT                                     2.1
## 44            CBT                                    -1.4
## 45            CBT                                     1.4
## 46            CBT                                    -0.3
## 47            CBT                                    -3.7
## 48            CBT                                    -0.8
## 49            CBT                                     2.4
## 50            CBT                                    12.6
## 51            CBT                                     1.9
## 52            CBT                                     3.9
## 53            CBT                                     0.1
## 54            CBT                                    15.4
## 55            CBT                                    -0.7
## 56             FT                                    11.4
## 57             FT                                    11.0
## 58             FT                                     5.5
## 59             FT                                     9.4
## 60             FT                                    13.6
## 61             FT                                    -2.9
## 62             FT                                    -0.1
## 63             FT                                     7.4
## 64             FT                                    21.5
## 65             FT                                    -5.3
## 66             FT                                    -3.8
## 67             FT                                    13.4
## 68             FT                                    13.1
## 69             FT                                     9.0
## 70             FT                                     3.9
## 71             FT                                     5.7
## 72             FT                                    10.7
colnames(anorexia_treat_df) <- c("Treat", "Dif")
head(anorexia_treat_df)
##   Treat   Dif
## 1  Cont  -0.5
## 2  Cont  -9.3
## 3  Cont  -5.4
## 4  Cont  12.3
## 5  Cont  -2.0
## 6  Cont -10.2
anorexia_treat_C_df<- subset(anorexia_treat_df, anorexia_treat_df$Treat == "Cont"&anorexia_treat_df$Dif >0)
head(anorexia_treat_C_df)
##    Treat  Dif
## 4   Cont 12.3
## 8   Cont 11.6
## 10  Cont  6.2
## 13  Cont  8.3
## 14  Cont  3.3
## 15  Cont 11.3