This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
R.version
## _
## platform x86_64-w64-mingw32
## arch x86_64
## os mingw32
## crt ucrt
## system x86_64, mingw32
## status
## major 4
## minor 3.3
## year 2024
## month 02
## day 29
## svn rev 86002
## language R
## version.string R version 4.3.3 (2024-02-29 ucrt)
## nickname Angel Food Cake
??Rcmdr
## starting httpd help server ... done
library(MASS)
data(anorexia)
print(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
## 7 Cont 87.3 75.1
## 8 Cont 75.1 86.7
## 9 Cont 80.6 73.5
## 10 Cont 78.4 84.6
## 11 Cont 77.6 77.4
## 12 Cont 88.7 79.5
## 13 Cont 81.3 89.6
## 14 Cont 78.1 81.4
## 15 Cont 70.5 81.8
## 16 Cont 77.3 77.3
## 17 Cont 85.2 84.2
## 18 Cont 86.0 75.4
## 19 Cont 84.1 79.5
## 20 Cont 79.7 73.0
## 21 Cont 85.5 88.3
## 22 Cont 84.4 84.7
## 23 Cont 79.6 81.4
## 24 Cont 77.5 81.2
## 25 Cont 72.3 88.2
## 26 Cont 89.0 78.8
## 27 CBT 80.5 82.2
## 28 CBT 84.9 85.6
## 29 CBT 81.5 81.4
## 30 CBT 82.6 81.9
## 31 CBT 79.9 76.4
## 32 CBT 88.7 103.6
## 33 CBT 94.9 98.4
## 34 CBT 76.3 93.4
## 35 CBT 81.0 73.4
## 36 CBT 80.5 82.1
## 37 CBT 85.0 96.7
## 38 CBT 89.2 95.3
## 39 CBT 81.3 82.4
## 40 CBT 76.5 72.5
## 41 CBT 70.0 90.9
## 42 CBT 80.4 71.3
## 43 CBT 83.3 85.4
## 44 CBT 83.0 81.6
## 45 CBT 87.7 89.1
## 46 CBT 84.2 83.9
## 47 CBT 86.4 82.7
## 48 CBT 76.5 75.7
## 49 CBT 80.2 82.6
## 50 CBT 87.8 100.4
## 51 CBT 83.3 85.2
## 52 CBT 79.7 83.6
## 53 CBT 84.5 84.6
## 54 CBT 80.8 96.2
## 55 CBT 87.4 86.7
## 56 FT 83.8 95.2
## 57 FT 83.3 94.3
## 58 FT 86.0 91.5
## 59 FT 82.5 91.9
## 60 FT 86.7 100.3
## 61 FT 79.6 76.7
## 62 FT 76.9 76.8
## 63 FT 94.2 101.6
## 64 FT 73.4 94.9
## 65 FT 80.5 75.2
## 66 FT 81.6 77.8
## 67 FT 82.1 95.5
## 68 FT 77.6 90.7
## 69 FT 83.5 92.5
## 70 FT 89.9 93.8
## 71 FT 86.0 91.7
## 72 FT 87.3 98.0
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 ...
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
## [6] Contr Contr Contr Contr Contr
## [11] Contr Contr Contr Contr Contr
## [16] Contr Contr Contr Contr Contr
## [21] Contr Contr Contr Contr Contr
## [26] Contr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr
## [31] Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr
## [36] Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr
## [41] Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr
## [46] Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr
## [51] Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr Cogn\nBeh\nTr
## [56] Fam Tr Fam Tr Fam Tr Fam Tr Fam Tr
## [61] Fam Tr Fam Tr Fam Tr Fam Tr Fam Tr
## [66] Fam Tr Fam Tr Fam Tr Fam Tr Fam Tr
## [71] Fam Tr Fam Tr
## Levels: Cogn\nBeh\nTr Contr Fam Tr
#Exportad los datos biopsy del paquete MASS a un archivo «.csv.»
library(MASS)
data(biopsy)
head (biopsy)
## ID V1 V2 V3 V4 V5 V6 V7 V8 V9 class
## 1 1000025 5 1 1 1 2 1 3 1 1 benign
## 2 1002945 5 4 4 5 7 10 3 2 1 benign
## 3 1015425 3 1 1 1 2 2 3 1 1 benign
## 4 1016277 6 8 8 1 3 4 3 7 1 benign
## 5 1017023 4 1 1 3 2 1 3 1 1 benign
## 6 1017122 8 10 10 8 7 10 9 7 1 malignant
write.csv(biopsy, file = "BIOPSY.csv")
#Exportad los datos melanoma del paquete MASS a archivos de tres diferentes formatos y comprobad que se han creado los diferentes archivos en los formatos y las rutas especificados. Podéis generar una captura de pantalla de su ubicación en la carpeta.
data(Melanoma)
head(Melanoma)
## time status sex age year thickness ulcer
## 1 10 3 1 76 1972 6.76 1
## 2 30 3 1 56 1968 0.65 0
## 3 35 2 1 41 1977 1.34 0
## 4 99 3 0 71 1968 2.90 0
## 5 185 1 1 52 1965 12.08 1
## 6 204 1 1 28 1971 4.84 1
resumen4<-summary(Melanoma$age)
resumen4
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4.00 42.00 54.00 52.46 65.00 95.00
#Instalar el paquete de office en R y cargar la librería
#resumen = summary(dataflores)
#capture.output(resumen, file="resumen.doc")
age_melanoma_resumen = summary(Melanoma$age)
capture.output(age_melanoma_resumen, file = "age_melanoma_resumen.doc" )
#Buscad un data frame en algún repositorio de datos de Biomedicina, descargad un conjunto de datos en «.csv» e importad este fichero a un documento R Markdown usando el código o el menú de importación de RStudio.
#determinación de maximos, minimos,
library(MASS)
data("birthwt")
View(birthwt)
edad_maxima<- max(birthwt$age)
edad_maxima
## [1] 45
edad_minima<- min(birthwt$age)
edad_minima
## [1] 14
rango<-range(birthwt$age)
rango
## [1] 14 45
lowest_weight_index<- which.min(birthwt$bwt)
estado_de_lamadre<- birthwt$smoke[lowest_weight_index]
estado_de_lamadre
## [1] 1
Lista_edad_max<- subset(birthwt, age>=45)
Lista_edad_max
## low age lwt race smoke ptl ht ui ftv bwt
## 226 0 45 123 1 0 0 0 0 1 4990
#Pesos de los newborn que sus madres visitaron 2x al médico
Lista_mothers_visit<- subset(birthwt, ftv<2, select=c(age,ftv,bwt) )
Lista_mothers_visit
## age ftv bwt
## 85 19 0 2523
## 87 20 1 2557
## 89 18 0 2600
## 91 21 0 2622
## 92 22 1 2637
## 93 17 1 2637
## 94 29 1 2663
## 95 26 0 2665
## 96 19 0 2722
## 97 19 1 2733
## 98 22 0 2751
## 100 18 0 2769
## 101 18 0 2769
## 102 15 0 2778
## 104 20 0 2807
## 105 28 1 2821
## 108 36 1 2836
## 109 28 0 2863
## 112 28 0 2877
## 113 17 0 2906
## 115 26 0 2920
## 116 17 1 2920
## 117 17 1 2920
## 118 24 1 2948
## 119 35 1 2948
## 120 25 1 2977
## 121 25 0 2977
## 125 27 0 2922
## 127 33 1 3033
## 130 23 1 3062
## 131 21 0 3062
## 132 18 0 3062
## 133 18 0 3062
## 135 19 0 3090
## 137 22 0 3090
## 138 22 1 3100
## 139 23 0 3104
## 140 22 0 3132
## 142 19 0 3175
## 143 16 0 3175
## 144 21 0 3203
## 145 30 0 3203
## 146 20 0 3203
## 147 17 0 3225
## 148 17 0 3225
## 150 24 0 3232
## 151 28 0 3234
## 154 26 0 3260
## 155 20 1 3274
## 160 20 1 3317
## 162 22 0 3317
## 164 23 1 3331
## 166 16 0 3374
## 167 16 0 3374
## 168 18 0 3402
## 169 25 1 3416
## 172 20 0 3444
## 173 23 0 3459
## 174 22 1 3460
## 175 32 0 3473
## 176 30 0 3544
## 177 20 0 3487
## 179 23 0 3544
## 180 17 0 3572
## 181 19 0 3572
## 182 23 0 3586
## 183 36 0 3600
## 184 22 1 3614
## 185 24 0 3614
## 187 19 0 3629
## 188 25 0 3637
## 189 16 0 3643
## 190 29 1 3651
## 191 29 1 3651
## 192 19 0 3651
## 193 19 0 3651
## 195 30 1 3699
## 196 24 1 3728
## 197 19 0 3756
## 199 24 0 3770
## 200 23 1 3770
## 201 20 0 3770
## 202 25 0 3790
## 203 30 1 3799
## 204 22 0 3827
## 208 18 1 3884
## 210 33 1 3912
## 211 20 0 3940
## 212 28 1 3941
## 213 14 0 3941
## 214 28 0 3969
## 216 16 1 3997
## 217 20 1 3997
## 218 26 0 4054
## 219 21 1 4054
## 220 22 0 4111
## 223 35 1 4174
## 224 19 0 4238
## 225 24 1 4593
## 226 45 1 4990
## 4 28 0 709
## 11 34 0 1135
## 13 25 0 1330
## 15 25 0 1474
## 16 27 0 1588
## 17 23 1 1588
## 18 24 1 1701
## 19 24 0 1729
## 20 21 1 1790
## 22 32 0 1818
## 23 19 0 1885
## 24 25 0 1893
## 25 16 1 1899
## 26 25 0 1928
## 29 24 0 1936
## 30 21 0 1970
## 31 20 0 2055
## 32 25 1 2055
## 34 19 0 2084
## 35 26 0 2084
## 36 24 0 2100
## 37 17 0 2125
## 42 22 1 2187
## 43 27 0 2187
## 44 20 0 2211
## 45 17 0 2225
## 46 25 1 2240
## 47 20 0 2240
## 49 18 0 2282
## 50 18 0 2296
## 51 20 0 2296
## 54 26 0 2325
## 56 31 1 2353
## 57 15 0 2353
## 59 23 1 2367
## 60 20 0 2381
## 61 24 0 2381
## 62 15 0 2381
## 63 23 0 2410
## 65 30 0 2410
## 67 22 1 2410
## 69 23 0 2424
## 75 26 1 2442
## 77 26 0 2466
## 78 14 0 2466
## 82 23 0 2495
## 83 17 0 2495
data("anorexia")
new_anorexia<- subset (anorexia, select = c(Prewt,Postwt))
new_anorexia
## 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
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Í
Edad_mayor22<- subset(Trat_Pulmón, Edad> 22)
Edad_mayor22
## 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Í
Elemento<-Trat_Pulmón[3,4]
Elemento
## [1] 79.3
Selection_27<- subset (Trat_Pulmón, Edad<27, select = -c(Alt))
Selection_27
## 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
data ("ChickWeight")
View(ChickWeight)
plot(ChickWeight$weight, main = "Diagrama de dispersión del peso", ylab= "Peso (gramos)")
boxplot(ChickWeight$Time, main = "Diagrama de Cajas del tiempo", ylab= "Tiempo (Semanas)", col = "yellow")
### Ejercicio 9, Trabajo con anorexia
data("anorexia")
View(anorexia)
with(anorexia, {diferencia_peso <- Prewt-Postwt
diferencia_peso
tratamiento <-anorexia$Treat
anorexia_treat_df<- data.frame(tratamiento, diferencia_peso)
anorexia_treat_df
anorexia_treat_df_con= subset(anorexia_treat_df, diferencia_peso<0 & tratamiento == "Cont" )
anorexia_treat_df_con})
## tratamiento diferencia_peso
## 4 Cont -12.3
## 8 Cont -11.6
## 10 Cont -6.2
## 13 Cont -8.3
## 14 Cont -3.3
## 15 Cont -11.3
## 21 Cont -2.8
## 22 Cont -0.3
## 23 Cont -1.8
## 24 Cont -3.7
## 25 Cont -15.9