EJERCICIO 1 Para comprobar qué paquete tenemos instalado en nuestra versión RStudio podemos ejecutar el comando library(). Una vez realizado, obtengo esta información con los paquetes instalados en una nueva ventana que se llam R packages available. En mi caso obtengo esta lista:
Packages in library ‘C:/Users/amayu/AppData/Local/R/win-library/4.6’:
bit Classes and Methods for Fast Memory-Efficient Boolean Selections bit64 A S3 Class for Vectors of 64bit Integers clipr Read and Write from the System Clipboard cpp11 A C++11 Interface for R’s C Interface crayon Colored Terminal Output farver High Performance Colour Space Manipulation ggplot2 Create Elegant Data Visualisations Using the Grammar of Graphics gtable Arrange ‘Grobs’ in Tables hms Pretty Time of Day isoband Generate Isolines and Isobands from Regularly Spaced Elevation Grids labeling Axis Labeling MASS Support Functions and Datasets for Venables and Ripley’s MASS prettyunits Pretty, Human Readable Formatting of Quantities progress Terminal Progress Bars RColorBrewer ColorBrewer Palettes readr Read Rectangular Text Data S7 An Object Oriented System Meant to Become a Successor to S3 and S4 scales Scale Functions for Visualization sm Smoothing Methods for Nonparametric Regression and Density Estimation tzdb Time Zone Database Information vioplot Violin Plot viridisLite Colorblind-Friendly Color Maps (Lite Version) vroom Read and Write Rectangular Text Data Quickly zoo S3 Infrastructure for Regular and Irregular Time Series (Z’s Ordered Observations)
Packages in library ‘D:/R-4.6.1/library’:
base The R Base Package base64enc Tools for ‘base64’ Encoding boot Bootstrap Functions bslib Custom ‘Bootstrap’ ‘Sass’ Themes for ‘shiny’ and ‘rmarkdown’ cachem Cache R Objects with Automatic Pruning class Functions for Classification cli Helpers for Developing Command Line Interfaces cluster “Finding Groups in Data”: Cluster Analysis Extended Rousseeuw et al. codetools Code Analysis Tools for R compiler The R Compiler Package datasets The R Datasets Package digest Create Compact Hash Digests of R Objects dplyr A Grammar of Data Manipulation evaluate Parsing and Evaluation Tools that Provide More Details than the Default fastmap Fast Data Structures fontawesome Easily Work with ‘Font Awesome’ Icons foreign Read Data Stored by ‘Minitab’, ‘S’, ‘SAS’, ‘SPSS’, ‘Stata’, ‘Systat’, ‘Weka’, ‘dBase’, … fs Cross-Platform File System Operations Based on ‘libuv’ generics Common S3 Generics not Provided by Base R Methods Related to Model Fitting glue Interpreted String Literals graphics The R Graphics Package grDevices The R Graphics Devices and Support for Colours and Fonts grid The Grid Graphics Package highr Syntax Highlighting for R Source Code htmltools Tools for HTML jquerylib Obtain ‘jQuery’ as an HTML Dependency Object jsonlite A Simple and Robust JSON Parser and Generator for R KernSmooth Functions for Kernel Smoothing Supporting Wand & Jones (1995) knitr A General-Purpose Package for Dynamic Report Generation in R lattice Trellis Graphics for R lifecycle Manage the Life Cycle of your Package Functions magrittr A Forward-Pipe Operator for R MASS Support Functions and Datasets for Venables and Ripley’s MASS Matrix Sparse and Dense Matrix Classes and Methods memoise ‘Memoisation’ of Functions methods Formal Methods and Classes mgcv Mixed GAM Computation Vehicle with Automatic Smoothness Estimation mime Map Filenames to MIME Types nlme Linear and Nonlinear Mixed Effects Models nnet Feed-Forward Neural Networks and Multinomial Log-Linear Models parallel Support for Parallel Computation in R pillar Coloured Formatting for Columns pkgconfig Private Configuration for ‘R’ Packages R6 Encapsulated Classes with Reference Semantics rappdirs Application Directories: Determine Where to Save Data, Caches, and Logs rlang Functions for Base Types and Core R and ‘Tidyverse’ Features rmarkdown Dynamic Documents for R rpart Recursive Partitioning and Regression Trees sass Syntactically Awesome Style Sheets (‘Sass’) spatial Functions for Kriging and Point Pattern Analysis splines Regression Spline Functions and Classes stats The R Stats Package stats4 Statistical Functions using S4 Classes survival Survival Analysis tcltk Tcl/Tk Interface tibble Simple Data Frames tidyselect Select from a Set of Strings tinytex Helper Functions to Install and Maintain TeX Live, and Compile LaTeX Documents tools Tools for Package Development translations The R Translations Package utf8 Unicode Text Processing utils The R Utils Package vctrs Vector Helpers withr Run Code ‘With’ Temporarily Modified Global State xfun Supporting Functions for Packages Maintained by ‘Yihui Xie’ yaml Methods to Convert R Data to YAML and Back.
Como podemos observar, yo ya tengo instalado el paquete MASS, pero para instalarlo he ejecutado el comando install.packages(“MASS”). Hacemos lo mismo para el paquete survival install.packages(“survival”). Para mostrar la información hemos de ejecutar data(package=“nombre”), y se abre una nueva ventana con la información respectiva de cada paquete.
Para buscar información sobre el paquete Rcmdr en la consola podemos ejecutar ??Rcmdr y se abre en la ventana de abajo a la derecha la información necesaria.
EJERCICIO 2 Para importar un documento.txt vamos a ir al panel superior derecho. En este seleccionamos Environment y seleccionamos en Import Dataset la opción deseada, en este caso From Text (readr), y de este modo también podemos importar uno .csv. Una vez importados los datos aparecen en la ventana superior derecha. En mi caso voy a importar los datos .txt del repositorio OUHSC Biostatistics Datasets. Para ello voy a ejecutar el comando:datos.txt <- read.table(“lab1.txt”, header = TRUE, sep = ““, dec =”.”). Para hacer un resumen de tres variables con el comando summary() lo ejecutaremos 3 veces, una con cada variable deseada: summary(datos\(x) summary(datos\)y) summary(datos$z) o summary(datos_txt[, c(“variable1”, “variable2”, “variable3”)])
Ahora importamos uno en csv. Yo he elegido unos datos sacados de kaggle sobre la diabetes. Ejecuto el comando: datos.csv<- read.csv(“diabetes.csv”, header = TRUE, sep = “,”) para importarlos. Ahora buscamos un fivenum() de dos variables relevantes.
EJERCICIO 3 Como ya tenemos el paquete instalado y cargado, ahora cargamos los datos “anorexia” usando el comando data(“anorexia”). Para observar los datos que tiene:
Para comprobar si tiene NA o NULL:
Por último, para cambiar los nombres de los valores de TREAT, hemos de pasar la variable categórica que está como factor a texto plano. Esto es para que no de problemas. Una vez hecho esto podemos cambiar los nombres de los valores dichos.
EJERCICIO 4
Para este ejercicio primero cargamos el paquete biopsy. Para exportarlo ejecutamos el comando: write.formato(datos, file=“lugar deseado/nombre”).
En mi caso primero he de exportar en csv: write.csv(biopsy, file=“D:/RSTUDIO/biopsy.csv). Hacemos lo mismo con los datos”Melanoma”.
EJERCICIO 5 Cargamos el conjunto de datos birthwt. Con el comando max(birthwt$age) podemos saber cuál es la edad máxima de las madres del conjunto de datos, nos da 45. Ahora ponemos el mismo comando pero con min para la mínima, nos da 14. Para el rango ejecutamos range(birthwt$age), nos da 14-45. Para saber si la madre cuyo niño era el de menor peso fuamaba vamos a hacer: birthwt\(smoke[birthwt\)bwt==min(birthwt$btw)] Nos da que Sí que fumaba. Ejecutamos un comando con la misma estructura para saber el peso del niño con la madre más mayor: birthwt$bwt[birthwt\(age==max(birthwt\)age)], nos da 4990.
Para saber los pesos de los niños cuyas madres visitaron menos de 2 veces al médico en el 1º Trimestre: list(birthwt\(bwt[birthwt\)ftv<2])
[[1]] [1] 2523 2557 2600 2622 2637 2637 2663 [8] 2665 2722 2733 2751 2769 2769 2778 [15] 2807 2821 2836 2863 2877 2906 2920 [22] 2920 2920 2948 2948 2977 2977 2922 [29] 3033 3062 3062 3062 3062 3090 3090 [36] 3100 3104 3132 3175 3175 3203 3203 [43] 3203 3225 3225 3232 3234 3260 3274 [50] 3317 3317 3331 3374 3374 3402 3416 [57] 3444 3459 3460 3473 3544 3487 3544 [64] 3572 3572 3586 3600 3614 3614 3629 [71] 3637 3643 3651 3651 3651 3651 3699 [78] 3728 3756 3770 3770 3770 3790 3799 [85] 3827 3884 3912 3940 3941 3941 3969 [92] 3997 3997 4054 4054 4111 4174 4238 [99] 4593 4990 709 1135 1330 1474 1588 [106] 1588 1701 1729 1790 1818 1885 1893 [113] 1899 1928 1936 1970 2055 2055 2084 [120] 2084 2100 2125 2187 2187 2211 2225 [127] 2240 2240 2282 2296 2296 2325 2353 [134] 2353 2367 2381 2381 2381 2410 2410 [141] 2410 2424 2442 2466 2466 2495 2495
EJERCICIO 6 matrix(c(anorexia\(Prewt, anorexia\)Postwt),ncol=2) [,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 El apartado a) Identificador Edad Sexo Peso Alt 1 I1 23 1 76.5 165 2 I2 24 2 81.2 154 5 I5 23 1 67.3 164 6 I6 25 2 78.6 175 7 I7 26 2 67.9 182 8 I8 24 2 100.2 165 11 I11 23 1 65.4 158 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 23 I23 26 1 65.8 189 25 I25 29 2 85.0 175 Fuma 1 SÍ 2 NO 5 NO 6 NO 7 NO 8 SÍ 11 NO 12 NO 13 SÍ 14 SÍ 17 NO 18 NO 19 SÍ 21 SÍ 22 NO 23 SÍ 25 SÍ
El apartado b) selec2<- Trat_Pulmon[3, 4] > selec2 [1] 79.3
El apartado c) selec3<- subset(Trat_Pulmon, Edad<27, select = -c(Alt)) > selec3 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 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
EJERCICIO 8 Cargamos los datos data(ChickWeight). Ahora ejecutamos los comandos respectivos para crear un gráfico de dispersión y de caja.
EJERCICIO 9 >rest<- c(anorexia\(Postwt-anorexia\)Prewt) > anorexia_treat_df<- data.frame(anorexia, rest) Treat Prewt Postwt rest 1 Cont 80.7 80.2 -0.5 2 Cont 89.4 80.1 -9.3 3 Cont 91.8 86.4 -5.4 4 Cont 74.0 86.3 12.3 5 Cont 78.1 76.1 -2.0 6 Cont 88.3 78.1 -10.2 7 Cont 87.3 75.1 -12.2 8 Cont 75.1 86.7 11.6 9 Cont 80.6 73.5 -7.1 10 Cont 78.4 84.6 6.2 11 Cont 77.6 77.4 -0.2 12 Cont 88.7 79.5 -9.2 13 Cont 81.3 89.6 8.3 14 Cont 78.1 81.4 3.3 15 Cont 70.5 81.8 11.3 16 Cont 77.3 77.3 0.0 17 Cont 85.2 84.2 -1.0 18 Cont 86.0 75.4 -10.6 19 Cont 84.1 79.5 -4.6 20 Cont 79.7 73.0 -6.7 21 Cont 85.5 88.3 2.8 22 Cont 84.4 84.7 0.3 23 Cont 79.6 81.4 1.8 24 Cont 77.5 81.2 3.7 25 Cont 72.3 88.2 15.9 26 Cont 89.0 78.8 -10.2 27 CBT 80.5 82.2 1.7 28 CBT 84.9 85.6 0.7 29 CBT 81.5 81.4 -0.1 30 CBT 82.6 81.9 -0.7 31 CBT 79.9 76.4 -3.5 32 CBT 88.7 103.6 14.9 33 CBT 94.9 98.4 3.5 34 CBT 76.3 93.4 17.1 35 CBT 81.0 73.4 -7.6 36 CBT 80.5 82.1 1.6 37 CBT 85.0 96.7 11.7 38 CBT 89.2 95.3 6.1 39 CBT 81.3 82.4 1.1 40 CBT 76.5 72.5 -4.0 41 CBT 70.0 90.9 20.9 42 CBT 80.4 71.3 -9.1 43 CBT 83.3 85.4 2.1 44 CBT 83.0 81.6 -1.4 45 CBT 87.7 89.1 1.4 46 CBT 84.2 83.9 -0.3 47 CBT 86.4 82.7 -3.7 48 CBT 76.5 75.7 -0.8 49 CBT 80.2 82.6 2.4 50 CBT 87.8 100.4 12.6 51 CBT 83.3 85.2 1.9 52 CBT 79.7 83.6 3.9 53 CBT 84.5 84.6 0.1 54 CBT 80.8 96.2 15.4 55 CBT 87.4 86.7 -0.7 56 FT 83.8 95.2 11.4 57 FT 83.3 94.3 11.0 58 FT 86.0 91.5 5.5 59 FT 82.5 91.9 9.4 60 FT 86.7 100.3 13.6 61 FT 79.6 76.7 -2.9 62 FT 76.9 76.8 -0.1 63 FT 94.2 101.6 7.4 64 FT 73.4 94.9 21.5 65 FT 80.5 75.2 -5.3 66 FT 81.6 77.8 -3.8 67 FT 82.1 95.5 13.4 68 FT 77.6 90.7 13.1 69 FT 83.5 92.5 9.0 70 FT 89.9 93.8 3.9 71 FT 86.0 91.7 5.7 72 FT 87.3 98.0 10.7
anorexia_treat_C_df<- subset(anorexia_treat_df, anorexia_treat_df\(Treat=="Cont"&anorexia_treat_df\)rest>0) head(anorexia_treat_C_df) Treat Prewt Postwt rest 4 Cont 74.0 86.3 12.3 8 Cont 75.1 86.7 11.6 10 Cont 78.4 84.6 6.2 13 Cont 81.3 89.6 8.3 14 Cont 78.1 81.4 3.3 15 Cont 70.5 81.8 11.3
EJERCICIO 10
PUBLICAR EN RPUB.