library()
#Para instalarlos se usa la función "install.packages()", y para cargarlos "library()"
library(MASS)
library(survival)
??Rcmdr ## Se nos abre en la ventana inferior derecha (ver imagen_1)
## starting httpd help server ... done
## Como necesitaremos la ruta, la podemos obtener así:
file.choose()
## [1] "C:\\Users\\54116508S\\Documents\\UOC\\SAD\\SAD_LAB1\\LAB1_R.Rmd"
## Ahora importamos el archivo de texto en un objeto llamado "data"
data <- read.table("C:\\Users\\54116508S\\Desktop\\UOC\\R\\PEC1\\LAB1\\doc.txt.txt")
View(data) ## Visualizamos el dataset
summary(data[c("time","status","sex")]) ## Buscamos un resumen de tres variables
## time status sex
## Min. : 10 Min. :1.00 Min. :0.0000
## 1st Qu.:1525 1st Qu.:1.00 1st Qu.:0.0000
## Median :2005 Median :2.00 Median :0.0000
## Mean :2153 Mean :1.79 Mean :0.3854
## 3rd Qu.:3042 3rd Qu.:2.00 3rd Qu.:1.0000
## Max. :5565 Max. :3.00 Max. :1.0000
datacsv <-read.csv("C:\\Users\\54116508S\\Desktop\\UOC\\R\\PEC1\\LAB1\\doc.csv.csv")
fivenum(datacsv$V1)
## [1] 1 2 4 6 10
fivenum(datacsv$V6)
## [1] 1 1 1 6 10
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
class(anorexia)
## [1] "data.frame"
table(is.na(anorexia)) ##Vemos si hay valores NA y cuántos
##
## FALSE
## 216
table(is.null(anorexia))##Vemos si hay valores null y cuántos
##
## FALSE
## 1
AnorexiaTr <- factor(anorexia$Treat,
levels=c("CBT","Cont","FT"),
labels=c("Cogn Beh Tr","Contr","Fam Tr"))
AnorexiaTr
## [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
install.packages("MASS")
library(MASS)
data("biopsy")
write.csv(biopsy,"biopsy.csv")
library(xlsx)
library(MASS)
data("Melanoma")
write.csv(Melanoma,"C:/Users/54116508S/Desktop/UOC/R/PEC1/LAB1/melanoma.csv")
write.table(Melanoma,"C:/Users/54116508S/Desktop/UOC/R/PEC1/LAB1/melanoma.txt")
write.xlsx(Melanoma,"C:/Users/54116508S/Desktop/UOC/R/PEC1/LAB1/melanoma.xlsx")
file.choose()
knitr::include_graphics("C:\\Users\\54116508S\\Desktop\\UOC\\R\\PEC1\\LAB1\\Captura_ubicacion_melanoma.PNG")
sum_melanoma=summary(Melanoma$age)
capture.output(sum_melanoma,file="C:/Users/54116508S/Desktop/UOC/R/PEC1/LAB1/resumen_melanoma.age.doc")
## Lo he importado utilizando el menú de importación de RStudio.
## También se puede hacer con código con la función "read.csv()"
doc1<-read.csv("C:\\Users\\54116508S\\Desktop\\UOC\\R\\PEC1\\LAB1\\delrepositorio.csv.csv")
Usando el conjunto de datos “birthwt”:
library(MASS)
data("birthwt")
max(birthwt$age)
## [1] 45
min(birthwt$age)
## [1] 14
range(birthwt$age) # Rango en intervalo
## [1] 14 45
max(birthwt$age)-min(birthwt$age) # Rango en un valor entero
## [1] 31
birthwt$smoke[birthwt$bwt == min(birthwt$bwt)] ## Con esta sintaxis estamos filtrando la variable "smoke" y extrayendo de ella el valor correspondiente a la fila donde la variable "bwt" es mínima. Como el resultado devuelto es 1, la respuesta es que sí fumaba.
## [1] 1
birthwt$bwt[birthwt$age==max(birthwt$age)] ## Extraemos el valor de edad max, y pedimos el peso (bwt) que le corresponde por fila.
## [1] 4990
birthwt$bwt[birthwt$ftv <= 2]
## [1] 2523 2557 2594 2600 2622 2637 2637 2663 2665 2722 2733 2751 2750 2769 2769
## [16] 2778 2807 2821 2835 2836 2863 2877 2877 2906 2920 2920 2920 2920 2948 2948
## [31] 2977 2977 2977 2977 2922 3005 3033 3042 3062 3062 3062 3062 3062 3090 3090
## [46] 3090 3100 3104 3132 3147 3175 3175 3203 3203 3203 3225 3225 3232 3232 3234
## [61] 3260 3274 3274 3317 3317 3317 3321 3331 3374 3374 3402 3416 3444 3459 3460
## [76] 3473 3544 3487 3544 3572 3572 3586 3600 3614 3614 3629 3629 3637 3643 3651
## [91] 3651 3651 3651 3699 3728 3756 3770 3770 3770 3790 3799 3827 3856 3860 3884
## [106] 3884 3912 3940 3941 3941 3969 3983 3997 3997 4054 4054 4111 4153 4167 4174
## [121] 4238 4593 4990 709 1021 1135 1330 1474 1588 1588 1701 1729 1790 1818 1885
## [136] 1893 1899 1928 1928 1928 1936 1970 2055 2055 2082 2084 2084 2100 2125 2187
## [151] 2187 2211 2225 2240 2240 2282 2296 2296 2325 2353 2353 2367 2381 2381 2381
## [166] 2410 2410 2410 2424 2438 2442 2466 2466 2466 2495 2495 2495
matriz<-matrix(c(anorexia$Prewt,anorexia$Postwt),
nrow=,
ncol=2)
head(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
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,]
a
## 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Í
b<-Trat_Pulmon[3,4]
b
## [1] 79.3
c<-subset(Trat_Pulmon, Edad<27, select=-c(Alt))
c
## 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
library(datasets)
df<-data.frame(ChickWeight)
plot(df$weight,
main="Chicken weight")
boxplot(df$Time,
main="Days since birth")
difwt<-(anorexia$Postwt)-(anorexia$Prewt)
anorexia_treat_df<-data.frame(anorexia$Treat,difwt)
head(anorexia_treat_df)
## anorexia.Treat difwt
## 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<-anorexia_treat_df[anorexia_treat_df$difwt<0 &
anorexia_treat_df$anorexia.Treat == "Cont",]
head(anorexia_treat_C_df)
## anorexia.Treat difwt
## 1 Cont -0.5
## 2 Cont -9.3
## 3 Cont -5.4
## 5 Cont -2.0
## 6 Cont -10.2
## 7 Cont -12.2
Id<-sample(1:30,30,replace=FALSE)
Edad<-sample(18:60,30,replace=TRUE)
Gene<-sample(1:2,30,replace=TRUE)
Treatments<-c("A","B","C")
Treat<-sample(Treatments,30,replace=TRUE)
Peso<-rnorm(30, mean = 65, sd = 10)
Alt<-rnorm(30, mean = 165, sd = 10)
datos<-data.frame(Id, Edad, Gene, Treat, Peso, Alt)
head(datos)
## Id Edad Gene Treat Peso Alt
## 1 3 60 2 B 62.49177 160.3955
## 2 22 56 1 C 75.85676 152.1845
## 3 20 58 1 B 85.03440 157.0260
## 4 27 28 2 A 78.60454 145.4121
## 5 9 18 1 C 68.38487 161.3649
## 6 24 48 1 A 59.16158 172.4873
summary(datos)
## Id Edad Gene Treat
## Min. : 1.00 Min. :18.00 Min. :1.000 Length:30
## 1st Qu.: 8.25 1st Qu.:28.25 1st Qu.:1.000 Class :character
## Median :15.50 Median :39.00 Median :2.000 Mode :character
## Mean :15.50 Mean :38.93 Mean :1.533
## 3rd Qu.:22.75 3rd Qu.:51.50 3rd Qu.:2.000
## Max. :30.00 Max. :60.00 Max. :2.000
## Peso Alt
## Min. :43.73 Min. :145.4
## 1st Qu.:58.60 1st Qu.:158.9
## Median :62.31 Median :164.8
## Mean :63.59 Mean :165.8
## 3rd Qu.:69.72 3rd Qu.:174.3
## Max. :85.03 Max. :189.7
datos$IMC<-datos$Peso/(datos$Alt/100)^2
head(datos)
## Id Edad Gene Treat Peso Alt IMC
## 1 3 60 2 B 62.49177 160.3955 24.29061
## 2 22 56 1 C 75.85676 152.1845 32.75318
## 3 20 58 1 B 85.03440 157.0260 34.48670
## 4 27 28 2 A 78.60454 145.4121 37.17465
## 5 9 18 1 C 68.38487 161.3649 26.26286
## 6 24 48 1 A 59.16158 172.4873 19.88499
*Cread dos data frames diferenciados para hombres y mujeres con dos nombres diferentes: Df_Hombres y Df_Mujeres
Df_hombres <- datos[datos$Gene == 2,]
Df_mujeres <- datos[datos$Gene == 1,]
head(Df_hombres)
## Id Edad Gene Treat Peso Alt IMC
## 1 3 60 2 B 62.49177 160.3955 24.29061
## 4 27 28 2 A 78.60454 145.4121 37.17465
## 7 26 43 2 C 73.70038 182.4978 22.12862
## 8 13 55 2 A 57.90986 165.0183 21.26611
## 10 6 50 2 B 45.80853 173.9372 15.14123
## 11 14 30 2 A 62.13062 159.6506 24.37612
head(Df_mujeres)
## Id Edad Gene Treat Peso Alt IMC
## 2 22 56 1 C 75.85676 152.1845 32.75318
## 3 20 58 1 B 85.03440 157.0260 34.48670
## 5 9 18 1 C 68.38487 161.3649 26.26286
## 6 24 48 1 A 59.16158 172.4873 19.88499
## 9 21 25 1 C 70.22773 176.3294 22.58702
## 12 7 38 1 B 62.08226 158.8108 24.61542
df_original<-rbind(Df_hombres,Df_mujeres)
df_original
## Id Edad Gene Treat Peso Alt IMC
## 1 3 60 2 B 62.49177 160.3955 24.29061
## 4 27 28 2 A 78.60454 145.4121 37.17465
## 7 26 43 2 C 73.70038 182.4978 22.12862
## 8 13 55 2 A 57.90986 165.0183 21.26611
## 10 6 50 2 B 45.80853 173.9372 15.14123
## 11 14 30 2 A 62.13062 159.6506 24.37612
## 13 10 21 2 C 52.38009 159.1162 20.68891
## 17 5 52 2 A 60.55910 149.1101 27.23738
## 19 11 23 2 C 58.73650 165.7305 21.38471
## 20 8 29 2 C 67.46093 169.6875 23.42894
## 22 17 33 2 A 58.08305 161.4187 22.29163
## 24 2 53 2 A 58.55159 182.3523 17.60825
## 26 16 32 2 C 43.72754 178.0829 13.78830
## 28 1 40 2 C 64.42249 148.7724 29.10670
## 29 19 53 2 C 53.66918 189.7036 14.91331
## 30 28 38 2 B 64.39636 164.4965 23.79842
## 2 22 56 1 C 75.85676 152.1845 32.75318
## 3 20 58 1 B 85.03440 157.0260 34.48670
## 5 9 18 1 C 68.38487 161.3649 26.26286
## 6 24 48 1 A 59.16158 172.4873 19.88499
## 9 21 25 1 C 70.22773 176.3294 22.58702
## 12 7 38 1 B 62.08226 158.8108 24.61542
## 14 12 21 1 B 66.75280 174.4798 21.92700
## 15 25 21 1 C 60.94290 177.0165 19.44893
## 16 15 46 1 A 74.99421 155.9119 30.85102
## 18 18 37 1 B 59.22828 165.0310 21.74694
## 21 30 18 1 A 52.04270 165.2789 19.05133
## 23 23 50 1 A 67.21015 158.0691 26.89929
## 25 4 52 1 B 70.17023 181.2978 21.34851
## 27 29 40 1 A 73.09094 163.1900 27.44582