EJERCICIO 1

Para comprobar los paquetes instalados en el sistema uso

head(installed.packages()[, c("Package", "Version")], n = 10)
##           Package     Version  
## base64enc "base64enc" "0.1-6"  
## bit       "bit"       "4.6.0"  
## bit64     "bit64"     "4.6.0-1"
## bslib     "bslib"     "0.10.0" 
## cachem    "cachem"    "1.1.0"  
## cli       "cli"       "3.6.5"  
## clipr     "clipr"     "0.8.0"  
## cpp11     "cpp11"     "0.5.3"  
## crayon    "crayon"    "1.5.3"  
## digest    "digest"    "0.6.39"
install.packages(c("MASS", "survival"))
## Installing packages into '/home/ignacio/R/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)

Ya teníamos los paquetes descargados en nuestra librería de R.

help(Rcmdr)
## No documentation for 'Rcmdr' in specified packages and libraries:
## you could try '??Rcmdr'
??Rcmdr

Usar el ??Rcmdr muestra la interfaz de ayuda de R.

EJERCICIO 2

a)

library("readr")
drug_interaction <- read_table("db_drug_interactions.txt")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   Drug = col_character(),
##   `1,Drug` = col_character(),
##   `2,Interaction` = col_character(),
##   Description = col_character()
## )
## Warning: 191541 parsing failures.
## row col  expected     actual                       file
##   1  -- 4 columns 8 columns  'db_drug_interactions.txt'
##   2  -- 4 columns 10 columns 'db_drug_interactions.txt'
##   3  -- 4 columns 10 columns 'db_drug_interactions.txt'
##   4  -- 4 columns 10 columns 'db_drug_interactions.txt'
##   5  -- 4 columns 8 columns  'db_drug_interactions.txt'
## ... ... ......... .......... ..........................
## See problems(...) for more details.
View(drug_interaction)
summary(drug_interaction)
##      Drug              1,Drug          2,Interaction      Description       
##  Length:191541      Length:191541      Length:191541      Length:191541     
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character

b)

structural_protein <- read_csv("pdb_data_no_dups.csv")
## Rows: 141401 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): structureId, classification, experimentalTechnique, macromoleculeTy...
## dbl (8): residueCount, resolution, structureMolecularWeight, crystallization...
## 
## ℹ 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.
summary(structural_protein)
##  structureId        classification     experimentalTechnique macromoleculeType 
##  Length:141401      Length:141401      Length:141401         Length:141401     
##  Class :character   Class :character   Class :character      Class :character  
##  Mode  :character   Mode  :character   Mode  :character      Mode  :character  
##                                                                                
##                                                                                
##                                                                                
##                                                                                
##   residueCount        resolution     structureMolecularWeight
##  Min.   :     0.0   Min.   : 0.480   Min.   :     314        
##  1st Qu.:   226.0   1st Qu.: 1.800   1st Qu.:   26129        
##  Median :   414.0   Median : 2.100   Median :   47478        
##  Mean   :   825.4   Mean   : 2.264   Mean   :  112079        
##  3rd Qu.:   820.0   3rd Qu.: 2.500   3rd Qu.:   94085        
##  Max.   :313236.0   Max.   :70.000   Max.   :97730536        
##                     NA's   :12812                            
##  crystallizationMethod crystallizationTempK densityMatthews densityPercentSol
##  Length:141401         Min.   :  4          Min.   : 0.00   Min.   : 0.00    
##  Class :character      1st Qu.:290          1st Qu.: 2.21   1st Qu.:44.37    
##  Mode  :character      Median :293          Median : 2.49   Median :50.50    
##                        Mean   :291          Mean   : 2.67   Mean   :51.35    
##                        3rd Qu.:295          3rd Qu.: 2.91   3rd Qu.:57.71    
##                        Max.   :398          Max.   :99.00   Max.   :92.00    
##                        NA's   :44362        NA's   :16677   NA's   :16652    
##  pdbxDetails           phValue        publicationYear
##  Length:141401      Min.   :  0.000   Min.   : 201   
##  Class :character   1st Qu.:  6.000   1st Qu.:2005   
##  Mode  :character   Median :  7.000   Median :2010   
##                     Mean   :  6.789   Mean   :2009   
##                     3rd Qu.:  7.500   3rd Qu.:2014   
##                     Max.   :724.000   Max.   :2018   
##                     NA's   :36291     NA's   :23799
fivenum(structural_protein$residueCount)
## [1]      0    226    414    820 313236
fivenum(structural_protein$structureMolecularWeight)
## [1]      314.38    26128.56    47477.79    94084.84 97730536.00

EJERCICIO 3

library("MASS")
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
summary(anorexia)
##   Treat        Prewt           Postwt      
##  CBT :29   Min.   :70.00   Min.   : 71.30  
##  Cont:26   1st Qu.:79.60   1st Qu.: 79.33  
##  FT  :17   Median :82.30   Median : 84.05  
##            Mean   :82.41   Mean   : 85.17  
##            3rd Qu.:86.00   3rd Qu.: 91.55  
##            Max.   :94.90   Max.   :103.60
# Valores perdidos
table(is.na(anorexia))
## 
## FALSE 
##   216
# valores nulos
table(is.null(anorexia))
## 
## FALSE 
##     1
anorexia_changed <- factor(anorexia$Treat,
                           levels=c("CBT", "Cont", "FT"),
                           labels=c("Cogn Beh Tr", "Contr", "Fam Tr"))
anorexia_changed
##  [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

a)

data("biopsy")

# Exportar el CSV
write.csv(biopsy, file="biopsy.csv")

b)

data("Melanoma")
write.table(Melanoma, file="melanoma.txt")

#library(xlsx)
#write.excel(Melanoma, file="melanoma.xslx")

c)

capture.output(summary(Melanoma$age), file="melanome_age.doc")

EJERCICIO 5

data("birthwt")

a)

max(birthwt$age)
## [1] 45

b)

min(birthwt$age)
## [1] 14

c)

sprintf("[%d, %d]", min(birthwt$age), max(birthwt$age))
## [1] "[14, 45]"
range(birthwt$age)
## [1] 14 45
rank<-max(birthwt$age)-min(birthwt$age) #solución del pdf
rank
## [1] 31

d)

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
summary(birthwt)
##       low              age             lwt             race      
##  Min.   :0.0000   Min.   :14.00   Min.   : 80.0   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:19.00   1st Qu.:110.0   1st Qu.:1.000  
##  Median :0.0000   Median :23.00   Median :121.0   Median :1.000  
##  Mean   :0.3122   Mean   :23.24   Mean   :129.8   Mean   :1.847  
##  3rd Qu.:1.0000   3rd Qu.:26.00   3rd Qu.:140.0   3rd Qu.:3.000  
##  Max.   :1.0000   Max.   :45.00   Max.   :250.0   Max.   :3.000  
##      smoke             ptl               ht                ui        
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.00000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:0.0000  
##  Median :0.0000   Median :0.0000   Median :0.00000   Median :0.0000  
##  Mean   :0.3915   Mean   :0.1958   Mean   :0.06349   Mean   :0.1481  
##  3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:0.00000   3rd Qu.:0.0000  
##  Max.   :1.0000   Max.   :3.0000   Max.   :1.00000   Max.   :1.0000  
##       ftv              bwt      
##  Min.   :0.0000   Min.   : 709  
##  1st Qu.:0.0000   1st Qu.:2414  
##  Median :0.0000   Median :2977  
##  Mean   :0.7937   Mean   :2945  
##  3rd Qu.:1.0000   3rd Qu.:3487  
##  Max.   :6.0000   Max.   :4990
birthwt$smoke[birthwt$bwt == min(birthwt$bwt)]
## [1] 1

e)

birthwt$bwt[birthwt$age == max(birthwt$age)]
## [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

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
matrix_anorexia <- matrix(c(anorexia$Prewt, anorexia$Postwt), ncol=2)
head(matrix_anorexia)
##      [,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

EJERCICIO 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")
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
##    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Í

a)

Trat_Pulmon[Trat_Pulmon$Edad > 22,]
##    Identificador Edad Sexo  Peso 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Í
resultado <- subset(Trat_Pulmon, Edad > 2)
resultado
##    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Í

b)

Trat_Pulmon[3,4]
## [1] 79.3
resultado <- subset(Trat_Pulmon, Edad < 27, select = -Alt)
resultado
##    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

a)

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

b)

plot(ChickWeight$weight, main="Peso de los pollitos")

### c)

boxplot(ChickWeight$Time, main="Time")

summary(ChickWeight$Time)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    4.00   10.00   10.72   16.00   21.00

EJERCICIO 9

data(anorexia)
anorexia_treat_df <- data.frame(Treat=anorexia$Treat, Diferencia=c(anorexia$Postwt-anorexia$Prewt))
head(anorexia_treat_df)
##   Treat Diferencia
## 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, Treat=="Cont" & Diferencia > 0)
head(anorexia_treat_C_df)
##    Treat Diferencia
## 4   Cont       12.3
## 8   Cont       11.6
## 10  Cont        6.2
## 13  Cont        8.3
## 14  Cont        3.3
## 15  Cont       11.3