Ejercicio 1:
Utilizando las funciones citadas en este Laboratorio, comprobad qué paquetes tenéis instalados en vuestra versión de RStudio e instalad el paquete MASS y el paquete Survival y comprobad la información que contienen. Buscad información sobre el paquete Rcmdr (R Commander) desde la consola.
#install.packages("MASS")
#install.packages("survival")
packageDescription("Rcmdr")
## Package: Rcmdr
## Version: 2.9-5
## Date: 2024-10-23
## Title: R Commander
## Authors@R: c(person("John", "Fox", role = c("aut", "cre"), email =
## "jfox@mcmaster.ca"), person("Milan", "Bouchet-Valat", role =
## "aut"), person("Manuel", "Munoz Marquez", role = "aut"),
## person("Liviu", "Andronic", role = "ctb"), person("Michael",
## "Ash", role = "ctb"), person("Theophilius", "Boye", role =
## "ctb"), person("Stefano", "Calza", role = "ctb"),
## person("Andy", "Chang", role = "ctb"), person("Vilmantas",
## "Gegzna", role = "ctb"), person("Philippe", "Grosjean", role =
## "ctb"), person("Richard", "Heiberger", role = "ctb"),
## person("Yoshinobu", "Kanda", role = "ctb"), person("Kosar",
## "Karimi Pour", role = "ctb"), person("G. Jay", "Kerns", role =
## "ctb"), person("Renaud", "Lancelot", role = "ctb"),
## person("Matthieu", "Lesnoff", role = "ctb"), person("Uwe",
## "Ligges", role = "ctb"), person("Samir", "Messad", role =
## "ctb"), person("Martin", "Maechler", role = "ctb"),
## person("Robert", "Muenchen", role = "ctb"), person("Duncan",
## "Murdoch", role = "ctb"), person("Erich", "Neuwirth", role =
## "ctb"), person("Dan", "Putler", role = "ctb"), person("Brian",
## "Ripley", role = "ctb"), person("Miroslav", "Ristic", role =
## "ctb"), person("Peter", "Wolf", role = "ctb"), person("Kevin",
## "Wright", role="ctb") )
## Depends: R (>= 3.5.0), grDevices, graphics, methods, stats, utils,
## splines, RcmdrMisc (>= 2.9-1), car (>= 3.1-0), effects (>=
## 4.0-3)
## Imports: tcltk, tcltk2 (>= 1.2-6), abind, relimp (>= 1.0-5), lme4,
## tools
## Suggests: aplpack, boot, colorspace, e1071, foreign, grid, Hmisc,
## knitr, lattice, leaps, lmtest, markdown, MASS, mgcv, multcomp
## (>= 0.991-2), nlme, nnet, nortest, readxl, rgl (>= 0.110.2),
## rmarkdown (>= 0.9.5), sem (>= 2.1-1)
## ByteCompile: yes
## Description: A platform-independent basic-statistics GUI (graphical
## user interface) for R, based on the tcltk package.
## License: GPL (>= 2)
## URL: https://github.com/RCmdr-Project/rcmdr, https://www.r-project.org,
## https://www.john-fox.ca/RCommander/index.html
## NeedsCompilation: no
## Packaged: 2024-10-24 00:46:34 UTC; johnfox
## Author: John Fox [aut, cre], Milan Bouchet-Valat [aut], Manuel Munoz
## Marquez [aut], Liviu Andronic [ctb], Michael Ash [ctb],
## Theophilius Boye [ctb], Stefano Calza [ctb], Andy Chang [ctb],
## Vilmantas Gegzna [ctb], Philippe Grosjean [ctb], Richard
## Heiberger [ctb], Yoshinobu Kanda [ctb], Kosar Karimi Pour
## [ctb], G. Jay Kerns [ctb], Renaud Lancelot [ctb], Matthieu
## Lesnoff [ctb], Uwe Ligges [ctb], Samir Messad [ctb], Martin
## Maechler [ctb], Robert Muenchen [ctb], Duncan Murdoch [ctb],
## Erich Neuwirth [ctb], Dan Putler [ctb], Brian Ripley [ctb],
## Miroslav Ristic [ctb], Peter Wolf [ctb], Kevin Wright [ctb]
## Maintainer: John Fox <jfox@mcmaster.ca>
## Repository: CRAN
## Date/Publication: 2024-10-24 17:10:13 UTC
## Built: R 4.4.3; ; 2025-10-08 04:55:11 UTC; windows
##
## -- File: C:/Users/sebab/AppData/Local/R/win-library/4.4/Rcmdr/Meta/package.rds
Ejercicio 2:
#Primero creamos un .txt
library(readr)
dataset<-read.table("C:/UOC_BI/PRIMER_SEMESTRE/S_A_D/RETO 1/ejercicio2.txt", header=TRUE)
#usé el comando de la asignatura Prob. Y Estadística (el del LAB1 no funcionaba)
resumen<-summary(dataset[c("edad","peso","estatura")])
resumen
## edad peso estatura
## Min. :19.0 Min. :60 Min. :165.0
## 1st Qu.:20.0 1st Qu.:68 1st Qu.:170.0
## Median :21.0 Median :70 Median :170.0
## Mean :23.4 Mean :72 Mean :172.4
## 3rd Qu.:27.0 3rd Qu.:77 3rd Qu.:175.0
## Max. :30.0 Max. :85 Max. :182.0
#csv
dataset2<-read.table("C:/UOC_BI/PRIMER_SEMESTRE/S_A_D/RETO 1/ejercicio2.csv", header=TRUE, sep=",")
five_num_edad<-fivenum(dataset2$edad)
five_num_peso<-fivenum(dataset2$peso)
five_num_edad
## [1] 19 20 21 27 30
five_num_peso
## [1] 60 68 70 77 85
Ejercicio 3:
A partir del conjunto de datos anorexia del paquete MASS, que corresponden a los datos de cambio de peso de pacientes jóvenes con anorexia, mostrad los tipos de datos que contiene y comprobad si existen valores NA y NULL. Para la variable Treat, transformad los valores «CBT», «Cont» y «FT» en «Cogn Beh Tr», «Contr» y «Fam Tr», respectivamente.
library(MASS)
data("anorexia")
#head(anorexia)
datatype<-str(anorexia[c("Treat","Prewt","Postwt")])
## '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 ...
datatype
## NULL
table(is.na(anorexia))
##
## FALSE
## 216
table(is.null(anorexia))
##
## FALSE
## 1
anorexia_F<-factor(anorexia$Treat, levels=c("CBT","Cont","FT"), labels=c("Cogn Beh Tr", "Contr", "Fam Tr"))
anorexia_F
## [1] Contr Contr Contr Contr Contr Contr
## [7] Contr Contr Contr Contr Contr Contr
## [13] Contr Contr Contr Contr Contr Contr
## [19] Contr Contr Contr Contr Contr Contr
## [25] Contr Contr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr
## [31] Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr
## [37] Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr
## [43] Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr
## [49] Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr Cogn Beh Tr
## [55] Cogn Beh Tr Fam Tr Fam Tr Fam Tr Fam Tr Fam Tr
## [61] Fam Tr Fam Tr Fam Tr Fam Tr Fam Tr Fam Tr
## [67] Fam Tr Fam Tr Fam Tr Fam Tr Fam Tr Fam Tr
## Levels: Cogn Beh Tr Contr Fam Tr
Ejercicio 4:
data("biopsy","Melanoma")
#install.packages("xlsx")
library(xlsx)
#head(biopsy)
#------Apartado A------------------------------------------
write.csv(biopsy, file="C:/UOC_BI/PRIMER_SEMESTRE/S_A_D/RETO 1/biopsy.csv")
#-----Apartado B-------------------------------------------
#head(Melanoma)
write.csv(Melanoma, file="C:/UOC_BI/PRIMER_SEMESTRE/S_A_D/RETO 1/Melanoma.csv")
write.table(Melanoma, file="C:/UOC_BI/PRIMER_SEMESTRE/S_A_D/RETO 1/Melanoma.txt", sep=" ")
write.xlsx(Melanoma, file="C:/UOC_BI/PRIMER_SEMESTRE/S_A_D/RETO 1/Melanoma.xlsx")
Edad<-summary(Melanoma$age)
capture.output(Edad, file="C:/UOC_BI/PRIMER_SEMESTRE/S_A_D/RETO 1/Edad_Melanoma.doc")
#------Apartado D------------------------------------------
#Ref: https://www.kaggle.com/datasets/jockeroika/life-style-data?resource=download
library(readr)
#LSDK: Life Style Data from Kaggle
LSDK<-read_csv("C:/UOC_BI/PRIMER_SEMESTRE/S_A_D/RETO 1/LSDK/Final_data.csv")
## Rows: 20000 Columns: 54
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (15): Gender, Workout_Type, meal_name, meal_type, diet_type, cooking_met...
## dbl (39): Age, Weight (kg), Height (m), Max_BPM, Avg_BPM, Resting_BPM, Sessi...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(LSDK)
Ejercicio 5: En el siguiente ejemplo veremos cómo utilizar diferentes operadores sobre el conjunto de datos birthwt, así como también algunas funciones que nos permiten obtener más información de las variables:
data("birthwt")
#View(birthwt)
#-----A-----
Max<-max(birthwt$age)
#----B----
Min<-min(birthwt$age)
#----C----
Ran<-c(Max-Min)
Max
## [1] 45
Min
## [1] 14
Ran
## [1] 31
#---D---
Mbwt<-birthwt[birthwt$bwt==min(birthwt$bwt),5]
Mbwt
## [1] 1
#---E----
Mxbrd<-birthwt[birthwt$age==max(birthwt$age), "bwt"]
Mxbrd
## [1] 4990
#---F---
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
Ejercicio 6: A partir del conjunto de datos anorexia trabajado en apartados anteriores, cread una matriz que tenga como columnas los valores de Prewt y Postwt, y cada fila sean los valores correspondientes para cada posición.
Anorexia<-c(anorexia$Prewt,anorexia$Postwt)
#anorexia$Prewt
#anorexia$Postwt
Matriz<-matrix(Anorexia, ncol=2)
Matriz
## [,1] [,2]
## [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
Ejercicio 7: a. Seleccionad los registros con edad > 22. b. Seleccionad el elemento 3 de la columna 4 del conjunto de datos (contando el identificador). c. Usad el comando subset() para seleccionar todas las filas que tienen una edad menor que 27 años y sin incluir la columna Alt.
#----Apartado A----
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")
Edad <-
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)
Sexo <-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 para mujeres y 2 para hombres
Peso <-
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Í")
Trat_Pulmon <- data.frame(Identificador,Edad,Sexo,Peso,Alt,Fuma)
Trat_Pulmon
Fuma<- c("SI","NO","SI","SI","NO","NO","NO","SI","SI","SI",
"NO","NO","SI","SI","SI","SI","NO","NO","SI","SI",
"SI","NO","SI","NO","SI")
Trat_Pulmon <- data.frame(Identificador,Edad,Sexo,Peso,Alt,Fuma)
Trat_Pulmon
edad22 <- Trat_Pulmon[Trat_Pulmon$Edad > 22, ]
edad22
#-----Apartado B-----
#head(Trat_Pulmon)
Trat_Pulmon[3,4]
## [1] 79.3
#-----Apartado C----
attach(Trat_Pulmon)
## The following objects are masked _by_ .GlobalEnv:
##
## Alt, Edad, Fuma, Identificador, Peso, Sexo
subset(Trat_Pulmon, select= -Alt, Trat_Pulmon$Edad<27)
detach(Trat_Pulmon)
Ejercicio 8: Incorporad el dataset ChickWeight que contiene información sobre el peso de 578 pollitos en gramos (weight), el tiempo desde la medición al nacer (Time), una variable identificadora de cada pollito (Chick) a partir del rango de peso y una variable factor con el tipo de dieta experimental que cada pollito recibió (Diet).
#----A-----
data("ChickWeight")
#head(ChickWeight)
#View(ChickWeight)
attach(ChickWeight)
#----B-----
plot(weight, main="Grafico del peso")
boxplot(Time, main="Grafico peso/tiempo")
detach(ChickWeight)
Ejercicio 9: A partir del conjunto de datos anorexia del paquete MASS, cread otro data frame que se llame anorexia_treat_df formado por Treat y por un vector nuevo calculado a partir de la diferencia Prewt-Postwt. De esta manera, nos quedará un data frame que contenga el tipo de tratamiento y el valor del peso ganado o perdido después de haber realizado el tratamiento. Seleccionad aquellos individuos que han ganado peso después del tratamiento y cread un nuevo conjunto llamado anorexia_treat_C_df que contenga solo los datos de aquellos que han seguido el tratamiento «Cont» y que han ganado peso después del tratamiento.
attach(anorexia)
PrPo<-c(Prewt-Postwt)
tratamiento<-anorexia[,1]
#tratamiento
anorexia_treat_df<-data.frame(tratamiento, "Diferenciapeso"= PrPo)
anorexia_treat_df
anorexia_treat_C_df<-anorexia_treat_df[PrPo>0 & tratamiento=="Cont",]
anorexia_treat_C_df
detach(anorexia)
Ejercicio 10: Entrad en RPubs y registraros. Crearos un perfil y subid un documento R Markdown. Los prerequisitos son tener instalado R y RStudio (v0.96.230 o más), y el paquete knitr (v0.5 o más).
Pasos que tenéis que seguir para publicar vuestro documento: