library()
install.packages("MASS", "survival")
library(help="MASS")
library(help="survival")
??Rcmdr
## starting httpd help server ... done
library(readr)
read.csv("exercici2.txt", sep="")
## id pais freq expt
## 1 1 1"espanya"10"si" NA NA
## 2 2 2"anglaterra"0"no" NA NA
## 3 3 3"brasil"1"si" NA NA
## 4 4 4"USA"2"si" NA NA
summary("ecercici2.csv")
## Length Class Mode
## 1 character character
library(readr)
read.csv("exercici2.csv")
## X id pais freq expt
## 1 1 1 espanya 10 si
## 2 2 2 anglaterra 0 no
## 3 3 3 brasil 1 si
## 4 4 4 USA 2 si
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
dim(anorexia)
## [1] 72 3
table(is.na(anorexia))
##
## FALSE
## 216
table(is.null(anorexia))
##
## FALSE
## 1
anorexia_M<-factor(anorexia$"Treat", levels=c("CBT", "Cont", "FT"), labels=c("Cogn Beh Tr", "Contr", "Fam Tr"))
anorexia_M
## [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
getwd()
## [1] "C:/Users/1553979/OneDrive - UAB/Documentos/Master Bioinfo i bioestadística/Programari per a l'anàlisi de dades/Lab1"
library(MASS)
data(biopsy)
write.csv(biopsy, file="biopsyds.csv")
library(MASS)
data("Melanoma")
write.csv(Melanoma, file="Melanoma.csv")
write.table(Melanoma, file="C:/Users/1553979/OneDrive - UAB/Documentos/Master Bioinfo i bioestadística/Programari per a l'anàlisi de dades/Melanoma.txt", sep="")
library(xlsx)
write.xlsx(Melanoma, file="C:/Users/1553979/OneDrive - UAB/Documentos/Master Bioinfo i bioestadística/Programari per a l'anàlisi de dades/Lab1/Exercici 3/Melanoma.xlsx")
library(MASS)
data(Melanoma)
Melanoma.age <- summary(Melanoma$age)
library(MASS)
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
attach(birthwt)
max(age)
## [1] 45
min(age)
## [1] 14
range(age)
## [1] 14 45
smoke[bwt==min(bwt)]
## [1] 1
bwt[age==max(age)]
## [1] 4990
list(bwt[ftv<2])
## [[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
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
Prewt <- c(anorexia$Prewt)
Postwt <- c(anorexia$Postwt)
Matriu <- matrix(Prewt, Postwt, ncol=2)
## Warning in matrix(Prewt, Postwt, ncol = 2): data length [72] is not a
## sub-multiple or multiple of the number of rows [80]
Matriu
## [,1] [,2]
## [1,] 80.7 80.6
## [2,] 89.4 78.4
## [3,] 91.8 77.6
## [4,] 74.0 88.7
## [5,] 78.1 81.3
## [6,] 88.3 78.1
## [7,] 87.3 70.5
## [8,] 75.1 77.3
## [9,] 80.6 85.2
## [10,] 78.4 86.0
## [11,] 77.6 84.1
## [12,] 88.7 79.7
## [13,] 81.3 85.5
## [14,] 78.1 84.4
## [15,] 70.5 79.6
## [16,] 77.3 77.5
## [17,] 85.2 72.3
## [18,] 86.0 89.0
## [19,] 84.1 80.5
## [20,] 79.7 84.9
## [21,] 85.5 81.5
## [22,] 84.4 82.6
## [23,] 79.6 79.9
## [24,] 77.5 88.7
## [25,] 72.3 94.9
## [26,] 89.0 76.3
## [27,] 80.5 81.0
## [28,] 84.9 80.5
## [29,] 81.5 85.0
## [30,] 82.6 89.2
## [31,] 79.9 81.3
## [32,] 88.7 76.5
## [33,] 94.9 70.0
## [34,] 76.3 80.4
## [35,] 81.0 83.3
## [36,] 80.5 83.0
## [37,] 85.0 87.7
## [38,] 89.2 84.2
## [39,] 81.3 86.4
## [40,] 76.5 76.5
## [41,] 70.0 80.2
## [42,] 80.4 87.8
## [43,] 83.3 83.3
## [44,] 83.0 79.7
## [45,] 87.7 84.5
## [46,] 84.2 80.8
## [47,] 86.4 87.4
## [48,] 76.5 83.8
## [49,] 80.2 83.3
## [50,] 87.8 86.0
## [51,] 83.3 82.5
## [52,] 79.7 86.7
## [53,] 84.5 79.6
## [54,] 80.8 76.9
## [55,] 87.4 94.2
## [56,] 83.8 73.4
## [57,] 83.3 80.5
## [58,] 86.0 81.6
## [59,] 82.5 82.1
## [60,] 86.7 77.6
## [61,] 79.6 83.5
## [62,] 76.9 89.9
## [63,] 94.2 86.0
## [64,] 73.4 87.3
## [65,] 80.5 80.7
## [66,] 81.6 89.4
## [67,] 82.1 91.8
## [68,] 77.6 74.0
## [69,] 83.5 78.1
## [70,] 89.9 88.3
## [71,] 86.0 87.3
## [72,] 87.3 75.1
## [73,] 80.7 80.6
## [74,] 89.4 78.4
## [75,] 91.8 77.6
## [76,] 74.0 88.7
## [77,] 78.1 81.3
## [78,] 88.3 78.1
## [79,] 87.3 70.5
## [80,] 75.1 77.3
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")
Edat <-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)
Sexe <-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 per a dones i 2 per a homes
Pes <-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Í")
Tract_Pulmo <- data.frame(Identificador,Edat,Sexe,Pes,Alt,Fuma)
Tract_Pulmo
## Identificador Edat Sexe Pes 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Í
subset(Tract_Pulmo,Edat>22)
## Identificador Edat Sexe Pes 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Í
Tract_Pulmo[3,4]
## [1] 79.3
subset(Tract_Pulmo, Edat<27, select=-c(Alt))
## Identificador Edat Sexe Pes 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)
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, type="p", main="Scatter plot", xlab="chicken ID", ylab="Weight")
boxplot(ChickWeight$Time, main="Boxplot", ylab="Time")
# Exercici 9
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
df <- c(anorexia$Prewt)-c(anorexia$Postwt)
anorexia_treat_df <- data.frame(Tract=c(anorexia$Treat), Result=df)
anorexia_treat_df
## Tract Result
## 1 Cont 0.5
## 2 Cont 9.3
## 3 Cont 5.4
## 4 Cont -12.3
## 5 Cont 2.0
## 6 Cont 10.2
## 7 Cont 12.2
## 8 Cont -11.6
## 9 Cont 7.1
## 10 Cont -6.2
## 11 Cont 0.2
## 12 Cont 9.2
## 13 Cont -8.3
## 14 Cont -3.3
## 15 Cont -11.3
## 16 Cont 0.0
## 17 Cont 1.0
## 18 Cont 10.6
## 19 Cont 4.6
## 20 Cont 6.7
## 21 Cont -2.8
## 22 Cont -0.3
## 23 Cont -1.8
## 24 Cont -3.7
## 25 Cont -15.9
## 26 Cont 10.2
## 27 CBT -1.7
## 28 CBT -0.7
## 29 CBT 0.1
## 30 CBT 0.7
## 31 CBT 3.5
## 32 CBT -14.9
## 33 CBT -3.5
## 34 CBT -17.1
## 35 CBT 7.6
## 36 CBT -1.6
## 37 CBT -11.7
## 38 CBT -6.1
## 39 CBT -1.1
## 40 CBT 4.0
## 41 CBT -20.9
## 42 CBT 9.1
## 43 CBT -2.1
## 44 CBT 1.4
## 45 CBT -1.4
## 46 CBT 0.3
## 47 CBT 3.7
## 48 CBT 0.8
## 49 CBT -2.4
## 50 CBT -12.6
## 51 CBT -1.9
## 52 CBT -3.9
## 53 CBT -0.1
## 54 CBT -15.4
## 55 CBT 0.7
## 56 FT -11.4
## 57 FT -11.0
## 58 FT -5.5
## 59 FT -9.4
## 60 FT -13.6
## 61 FT 2.9
## 62 FT 0.1
## 63 FT -7.4
## 64 FT -21.5
## 65 FT 5.3
## 66 FT 3.8
## 67 FT -13.4
## 68 FT -13.1
## 69 FT -9.0
## 70 FT -3.9
## 71 FT -5.7
## 72 FT -10.7
anorexia_treat_C_df <- subset(anorexia_treat_df, Tract=="Cont", Result>0)