# Comprovem quins paquets tenim instal·lats
library()
# Instal·lem el paquet MASS i el Survival
library(MASS)
library(survival)
# Comprovem la infor que contenen
packageDescription("MASS")
## Package: MASS
## Priority: recommended
## Version: 7.3-65
## Date: 2025-02-19
## Revision: $Rev: 3681 $
## Depends: R (>= 4.4.0), grDevices, graphics, stats, utils
## Imports: methods
## Suggests: lattice, nlme, nnet, survival
## Authors@R: c(person("Brian", "Ripley", role = c("aut", "cre", "cph"),
## email = "Brian.Ripley@R-project.org"), person("Bill",
## "Venables", role = c("aut", "cph")), person(c("Douglas", "M."),
## "Bates", role = "ctb"), person("Kurt", "Hornik", role = "trl",
## comment = "partial port ca 1998"), person("Albrecht",
## "Gebhardt", role = "trl", comment = "partial port ca 1998"),
## person("David", "Firth", role = "ctb", comment = "support
## functions for polr"))
## Description: Functions and datasets to support Venables and Ripley,
## "Modern Applied Statistics with S" (4th edition, 2002).
## Title: Support Functions and Datasets for Venables and Ripley's MASS
## LazyData: yes
## ByteCompile: yes
## License: GPL-2 | GPL-3
## URL: http://www.stats.ox.ac.uk/pub/MASS4/
## Contact: <MASS@stats.ox.ac.uk>
## NeedsCompilation: yes
## Packaged: 2025-02-19 08:49:43 UTC; ripley
## Author: Brian Ripley [aut, cre, cph], Bill Venables [aut, cph], Douglas
## M. Bates [ctb], Kurt Hornik [trl] (partial port ca 1998),
## Albrecht Gebhardt [trl] (partial port ca 1998), David Firth
## [ctb] (support functions for polr)
## Maintainer: Brian Ripley <Brian.Ripley@R-project.org>
## Repository: CRAN
## Date/Publication: 2025-02-28 17:44:52 UTC
## Built: R 4.5.2; x86_64-w64-mingw32; 2026-02-22 01:41:28 UTC; windows
## Archs: x64
## RemoteType: standard
## RemotePkgRef: MASS
## RemoteRef: MASS
## RemoteRepos: https://cran.rstudio.com
## RemoteSha: 7.3-65
##
## -- File: C:/Users/joshu/AppData/Local/R/win-library/4.5/MASS/Meta/package.rds
packageDescription("Survival")
## Warning in packageDescription("Survival"): no package 'Survival' was found
## [1] NA
# Volem buscar informació sobre Rcmdr
help(Rcmdr)
## No documentation for 'Rcmdr' in specified packages and libraries:
## you could try '??Rcmdr'
# Importem un .txt que ja es troba en el nostre Working Directory i establim
# la primera fila com els noms de les columnes
library(readr)
Pacients <- read.table("pacients.txt", header = TRUE)
# resum de tres variables
summary(Pacients[c("edat", "pes", "altura")])
## edat pes altura
## Min. :23.00 Min. :60.00 Min. :165.0
## 1st Qu.:27.25 1st Qu.:70.50 1st Qu.:168.2
## Median :30.00 Median :76.00 Median :171.0
## Mean :31.60 Mean :77.20 Mean :172.6
## 3rd Qu.:34.50 3rd Qu.:83.75 3rd Qu.:177.2
## Max. :45.00 Max. :95.00 Max. :182.0
# Importem un .csv que també es troba en el Working Directory. 1a fila = noms columna
empresa <- read.csv("empresa.csv", header = TRUE)
# calculem els mínim, Q1, Q2, Q3, i el màxim amb la funció fivenum()
fivenum(empresa$ingressos)
## [1] 1200 1600 1850 2100 2300
fivenum(empresa$despeses)
## [1] 800 950 1125 1250 1400
# Mirem el tipus de dades del df anorexia (el paquet MASS s'ha carregat anteriorment)
str(anorexia)
## 'data.frame': 72 obs. of 3 variables:
## $ Treat : Factor w/ 3 levels "CBT","Cont","FT": 2 2 2 2 2 2 2 2 2 2 ...
## $ Prewt : num 80.7 89.4 91.8 74 78.1 88.3 87.3 75.1 80.6 78.4 ...
## $ Postwt: num 80.2 80.1 86.4 86.3 76.1 78.1 75.1 86.7 73.5 84.6 ...
# Comrpovem quants NAs hi ha
table(is.na(anorexia))
##
## FALSE
## 216
# Comprovem si anorexia està buid (és NULL)
table(is.null(anorexia))
##
## FALSE
## 1
# Mirem l'ordre dels levels de la variable treat i els hi canviem els noms
levels(anorexia$Treat)
## [1] "CBT" "Cont" "FT"
levels(anorexia$Treat) <- c("Cogn Beh Tr", "Contr", "Fam Tr")
# Exportem les dades de biopsya a un .csv (el paquet MASS s'ha carregat anteriorment)
write.csv(biopsy, file="biopsy.csv")
# Exportem melanoma a formats .csv, .txt, i .xlsx
data("Melanoma")
write.table(Melanoma, file="melanoma.txt", sep="")
write.csv(Melanoma, file="melanoma.csv")
# Carreguem el paquet openxlsx i exportem
library(openxlsx)
write.xlsx(Melanoma, file="melanoma.xlsx")
capture.output(summary(Melanoma$age), file="resum age Melanoma.doc")
data("birthwt")
max(birthwt$age) #a
## [1] 45
min(birthwt$age) #b
## [1] 14
rang <- (max(birthwt$age)-min(birthwt$age)) #c
#Fumava la mare del nadó amb pes mínim?
which.min(birthwt$bwt)
## [1] 131
isTRUE(birthwt[which.min(birthwt$bwt),"smoke"] == 1)
## [1] TRUE
# Generem la llista del pes dels nadons on birthwt$ftv < 2
llista <- list(subset(birthwt, birthwt$ftv < 2)[,"bwt"])
# Generem una matriu a partir del data set anorexia agafant les columnes Prewt
# i Postwt
matriu <- as.matrix(anorexia[,c("Prewt", "Postwt")])
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Í
# registres amb Edat > 22
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Í
# element 3 de la columna 4
Tract_Pulmo[3,4]
## [1] 79.3
# files amb edat < 27 i sense Alt
subset(Tract_Pulmo, Edat < 27, select = -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
data("ChickWeight")
# Generem diagrama de dispersió de weight
plot(ChickWeight$weight)
# Generem diagrama de caixa de Time
boxplot(ChickWeight$Time)
# Exercici 9
data("anorexia")
# nou df amb les diferencies de pes post i pre tractament
dif_postpre <- c(anorexia$Postwt - anorexia$Prewt)
anorexia_treat_df <- data.frame(anorexia$Treat, dif_postpre)
# seleccionem els individus que han seguit el tractament Cont i han guanyat pes
anorexia_treat_C_df <- subset(anorexia_treat_df, dif_postpre > 0 & anorexia.Treat == "Cont")