library()
options(repos = c(CRAN = "https://cran.r-project.org"))
install.packages("MASS")
## Installing package into 'C:/Users/efuen/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'MASS' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\efuen\AppData\Local\Temp\RtmpM1xv2R\downloaded_packages
install.packages("survival")
## Installing package into 'C:/Users/efuen/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'survival' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\efuen\AppData\Local\Temp\RtmpM1xv2R\downloaded_packages
packageDescription("MASS")
## Package: MASS
## Priority: recommended
## Version: 7.3-60.0.1
## Date: 2024-01-12
## Revision: $Rev: 3621 $
## Depends: R (>= 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 = "ripley@stats.ox.ac.uk"), person("Bill", "Venables",
## role = "ctb"), 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"))
## 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: 2024-01-13 12:39:26 UTC; ripley
## Author: Brian Ripley [aut, cre, cph], Bill Venables [ctb], Douglas M.
## Bates [ctb], Kurt Hornik [trl] (partial port ca 1998), Albrecht
## Gebhardt [trl] (partial port ca 1998), David Firth [ctb]
## Maintainer: Brian Ripley <ripley@stats.ox.ac.uk>
## Repository: CRAN
## Date/Publication: 2024-01-13 13:36:16 UTC
## Built: R 4.3.3; x86_64-w64-mingw32; 2024-03-07 01:49:20 UTC; windows
## Archs: x64
##
## -- File: C:/Users/efuen/AppData/Local/R/win-library/4.3/MASS/Meta/package.rds
packageDescription("survival")
## Title: Survival Analysis
## Priority: recommended
## Package: survival
## Version: 3.5-8
## Date: 2024-02-13
## Depends: R (>= 3.5.0)
## Imports: graphics, Matrix, methods, splines, stats, utils
## LazyData: Yes
## LazyDataCompression: xz
## ByteCompile: Yes
## Authors@R: c(person(c("Terry", "M"), "Therneau",
## email="therneau.terry@mayo.edu", role=c("aut", "cre")),
## person("Thomas", "Lumley", role=c("ctb", "trl"),
## comment="original S->R port and R maintainer until 2009"),
## person("Atkinson", "Elizabeth", role="ctb"), person("Crowson",
## "Cynthia", role="ctb"))
## Description: Contains the core survival analysis routines, including
## definition of Surv objects, Kaplan-Meier and Aalen-Johansen
## (multi-state) curves, Cox models, and parametric accelerated
## failure time models.
## License: LGPL (>= 2)
## URL: https://github.com/therneau/survival
## NeedsCompilation: yes
## Packaged: 2024-02-13 23:24:58 UTC; therneau
## Author: Terry M Therneau [aut, cre], Thomas Lumley [ctb, trl] (original
## S->R port and R maintainer until 2009), Atkinson Elizabeth
## [ctb], Crowson Cynthia [ctb]
## Maintainer: Terry M Therneau <therneau.terry@mayo.edu>
## Repository: CRAN
## Date/Publication: 2024-02-14 10:20:23 UTC
## Built: R 4.3.3; x86_64-w64-mingw32; 2024-03-07 02:48:49 UTC; windows
## Archs: x64
##
## -- File: C:/Users/efuen/AppData/Local/R/win-library/4.3/survival/Meta/package.rds
??Rcmdr
## starting httpd help server ... done
# a) Importamos un archivo .txt y llamamos la resumen de las tres primeras variables
production_month <- read.csv("~/production_month.txt", sep="")
summary(production_month[, c(2,3,4)])
## generation.fossil.brown.coal.lignite generation.fossil.gas
## Min. :124.4 Min. :4267
## 1st Qu.:335.2 1st Qu.:4931
## Median :469.0 Median :5472
## Mean :447.8 Mean :5619
## 3rd Qu.:576.1 3rd Qu.:6187
## Max. :695.3 Max. :8527
## generation.fossil.hard.coal
## Min. :2157
## 1st Qu.:3756
## Median :4205
## Mean :4255
## 3rd Qu.:4875
## Max. :6748
# b) Importamos un archivo .csv y llamamos al fivenum() de dos varibales relevantes
production_day <- read.csv("~/production_day.csv")
fivenum(production_day$generation.biomass)
## [1] 0 333 367 433 592
fivenum(production_day$generation.fossil.brown.coal.lignite)
## [1] 0 0 509 757 999
Primero cargamos el data set ‘anorexia’ del paquete ‘MASS’, vemos el encabezado para saber qué variables contiene y averiguamos si tiene algún valor NA o NULL con las funciones any() e is.na() o is.null(), respectivamente:
library(MASS)
## Warning: package 'MASS' was built under R version 4.3.3
data("anorexia")
head(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
any(is.na(anorexia))
## [1] FALSE
any(is.null(anorexia))
## [1] FALSE
Para cambiar el nombre de los niveles de la variable Treat, hacemos lo siguiente:
new_names_Treat <- c("Cogn Beh Tr", "Contr", "Fam Tr") #Creamos un vector con los nuevos nombres
anorexia$Treat <- factor(anorexia$Treat, levels = c("CBT", "Cont", "FC"), labels = new_names_Treat) # Cambiamos las etiquetas
levels(anorexia$Treat) # Comprobamos que se han cambiado correctamente
## [1] "Cogn Beh Tr" "Contr" "Fam Tr"
data("biopsy")
write.csv(biopsy, "biopsy.csv")
data("Melanoma")
write.csv(Melanoma, "melanoma.csv")
install.packages("xlsx")
## Installing package into 'C:/Users/efuen/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'xlsx' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\efuen\AppData\Local\Temp\RtmpM1xv2R\downloaded_packages
library(xlsx)
## Warning: package 'xlsx' was built under R version 4.3.3
write.xlsx(Melanoma, "melanoma.xlsx")
write.csv(Melanoma, "melanoma.txt")
#### c) Generamos un resumen de la
variable age de melanoma y guardamos la salida que
aparece en un documento .doc:
age_summary <- summary(Melanoma$age)
capture.output(age_summary, file="age_summary.doc")
Descargamos el data set birth_estriol.csv de la colección de Vanderbilt Biostatistics Datasets, del repositorio de Dave Thombson. Luego lo importamos al documento con el siguiente código:
birth_estriol <- read.csv("C:/Users/efuen/Downloads/birth_estriol.csv", quote="")
data(birthwt)
head(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
max(birthwt$age)
## [1] 45
min(birthwt$age)
## [1] 14
range(birthwt$age)
## [1] 14 45
which.min(birthwt$bwt)
## [1] 131
birthwt[which.min(birthwt$bwt), 5] # La columna 5 es la variable smoke
## [1] 1
El valor 1 de la variable smoke corresponde a sí, por lo que la madre cuyo recién nacido era el de menor peso sí que fumaba.
birthwt[which.max(birthwt$age), 10] # La columna 10 es la variable bwt
## [1] 4990
El recién nacido de la madre de mayor edad pesó 4990 g.
ftv_1 <- subset(birthwt, ftv<2)
lista_pesos <- list(ftv_1$bwt)
lista_pesos
## [[1]]
## [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
anorexia_matrix <- as.matrix(anorexia[, c(2,3)])
anorexia_matrix # Verificamos que se haya creado correctamente
## 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
class(anorexia_matrix) # Comprobamos que sea de clase matrix
## [1] "matrix" "array"
Creamos el data frame con el código del enunciado.
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_Pulmón <- data.frame(Identificador,Edad,Sexo,Peso,Alt,Fuma)
Trat_Pulmón
## Identificador Edad Sexo Peso 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Í
mayores22 <- Trat_Pulmón[Edad>22, ]
min(mayores22$Edad) # Comprobamos que se haya filtrado correctamente
## [1] 23
Trat_Pulmón[3, 4]
## [1] 79.3
subset <- subset(Trat_Pulmón, Edad<27, select = -c(Alt))
head(subset)
## Identificador Edad Sexo Peso 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
max(subset$Edad)
## [1] 26
library("datasets")
data("ChickWeight")
head(ChickWeight)
## weight Time Chick Diet
## 1 42 0 1 1
## 2 51 2 1 1
## 3 59 4 1 1
## 4 64 6 1 1
## 5 76 8 1 1
## 6 93 10 1 1
plot(ChickWeight$weight, ChickWeight$Chick)
boxplot(ChickWeight$Time, ChickWeight$Chick)
anorexia_treat_df <- data.frame(
Treat = anorexia$Treat,
Weight_diff = anorexia$Postwt-anorexia$Prewt)
anorexia_treat_df
## Treat Weight_diff
## 1 Contr -0.5
## 2 Contr -9.3
## 3 Contr -5.4
## 4 Contr 12.3
## 5 Contr -2.0
## 6 Contr -10.2
## 7 Contr -12.2
## 8 Contr 11.6
## 9 Contr -7.1
## 10 Contr 6.2
## 11 Contr -0.2
## 12 Contr -9.2
## 13 Contr 8.3
## 14 Contr 3.3
## 15 Contr 11.3
## 16 Contr 0.0
## 17 Contr -1.0
## 18 Contr -10.6
## 19 Contr -4.6
## 20 Contr -6.7
## 21 Contr 2.8
## 22 Contr 0.3
## 23 Contr 1.8
## 24 Contr 3.7
## 25 Contr 15.9
## 26 Contr -10.2
## 27 Cogn Beh Tr 1.7
## 28 Cogn Beh Tr 0.7
## 29 Cogn Beh Tr -0.1
## 30 Cogn Beh Tr -0.7
## 31 Cogn Beh Tr -3.5
## 32 Cogn Beh Tr 14.9
## 33 Cogn Beh Tr 3.5
## 34 Cogn Beh Tr 17.1
## 35 Cogn Beh Tr -7.6
## 36 Cogn Beh Tr 1.6
## 37 Cogn Beh Tr 11.7
## 38 Cogn Beh Tr 6.1
## 39 Cogn Beh Tr 1.1
## 40 Cogn Beh Tr -4.0
## 41 Cogn Beh Tr 20.9
## 42 Cogn Beh Tr -9.1
## 43 Cogn Beh Tr 2.1
## 44 Cogn Beh Tr -1.4
## 45 Cogn Beh Tr 1.4
## 46 Cogn Beh Tr -0.3
## 47 Cogn Beh Tr -3.7
## 48 Cogn Beh Tr -0.8
## 49 Cogn Beh Tr 2.4
## 50 Cogn Beh Tr 12.6
## 51 Cogn Beh Tr 1.9
## 52 Cogn Beh Tr 3.9
## 53 Cogn Beh Tr 0.1
## 54 Cogn Beh Tr 15.4
## 55 Cogn Beh Tr -0.7
## 56 <NA> 11.4
## 57 <NA> 11.0
## 58 <NA> 5.5
## 59 <NA> 9.4
## 60 <NA> 13.6
## 61 <NA> -2.9
## 62 <NA> -0.1
## 63 <NA> 7.4
## 64 <NA> 21.5
## 65 <NA> -5.3
## 66 <NA> -3.8
## 67 <NA> 13.4
## 68 <NA> 13.1
## 69 <NA> 9.0
## 70 <NA> 3.9
## 71 <NA> 5.7
## 72 <NA> 10.7
anorexia_treat_C_df <- subset(anorexia_treat_df, Treat=="Contr" & Weight_diff >0)
anorexia_treat_C_df
## Treat Weight_diff
## 4 Contr 12.3
## 8 Contr 11.6
## 10 Contr 6.2
## 13 Contr 8.3
## 14 Contr 3.3
## 15 Contr 11.3
## 21 Contr 2.8
## 22 Contr 0.3
## 23 Contr 1.8
## 24 Contr 3.7
## 25 Contr 15.9
installed.packages("knitr", dependencies=TRUE)
## Package LibPath Version Priority Depends Imports LinkingTo Suggests
## Enhances License License_is_FOSS License_restricts_use OS_type Archs
## MD5sum NeedsCompilation Built
Enlace de mi página de RPubs