Solución

1.8

Access the data from url http://www.stat.berkeley.edu/users/statlabs/data/babies.data and store the information in an object named BABIES using the function read.table().A description of the variables can be found at http://www.stat.berkeley.edu/users/statlabs/labs.html. These data are a subset from a much larger study dealing with child health and development.

Nombre de la Variable Descripción
bwt Fecha de Nacimiento del Recién Nacido [Oz]
gestation Duración del embarazo [Dias]
parity Valores entre 0 y 1 dependiendo si es el primer hijo o no
age Edad de la Madre [Años]
height Altura de la Madre [in]
weight Peso de la Madre [lb]
smoke Estado de Fumadora de la Madre (0= No Ahora - 1=si Ahora)

a)

The variables bwt, gestation, parity, age, height, weight, and smoke use values of 999, 999, 9, 99, 99, 999, and 9, respectively, to denote “unknown.” \(R\) uses NA to denote a missing or unavailable value. Recode the missing values in BABIES. Hint: use something similar to BABIES\(bwt[BABIES\)bwt == 999] = NA. + Rta/:

BABIES <- read.table('C:/Users/flex/Downloads/babies.data')
names(BABIES) <- sapply(BABIES, "[", 1)
BABIES <- BABIES[-1,]
BABIES$bwt[BABIES$bwt== 999] = NA
BABIES$gestation[BABIES$gestation == 999] = NA
BABIES$parity[BABIES$parity == 9] = NA
BABIES$age[BABIES$age == 99] = NA
BABIES$height[BABIES$height == 99] = NA
BABIES$weight[BABIES$weight == 999] = NA
BABIES$smoke[BABIES$smoke == 9] = NA

summary(BABIES)
##      bwt             gestation            parity              age           
##  Length:1236        Length:1236        Length:1236        Length:1236       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##     height             weight             smoke          
##  Length:1236        Length:1236        Length:1236       
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character
head(BABIES,10)
##    bwt gestation parity age height weight smoke
## 2  120       284      0  27     62    100     0
## 3  113       282      0  33     64    135     0
## 4  128       279      0  28     64    115     1
## 5  123      <NA>      0  36     69    190     0
## 6  108       282      0  23     67    125     1
## 7  136       286      0  25     62     93     0
## 8  138       244      0  33     62    178     0
## 9  132       245      0  23     65    140     0
## 10 120       289      0  25     62    125     0
## 11 143       299      0  30     66    136     1

b)

Use the function na.omit() to create a “clean” data set that removes subjects if any observations on the subject are “unknown.” Store the modified data frame in a data frame named CLEAN. + Rta/:

CLEAN=na.omit(BABIES)
summary(CLEAN)
##      bwt             gestation            parity              age           
##  Length:1174        Length:1174        Length:1174        Length:1174       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##     height             weight             smoke          
##  Length:1174        Length:1174        Length:1174       
##  Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character
head(CLEAN,10)
##    bwt gestation parity age height weight smoke
## 2  120       284      0  27     62    100     0
## 3  113       282      0  33     64    135     0
## 4  128       279      0  28     64    115     1
## 6  108       282      0  23     67    125     1
## 7  136       286      0  25     62     93     0
## 8  138       244      0  33     62    178     0
## 9  132       245      0  23     65    140     0
## 10 120       289      0  25     62    125     0
## 11 143       299      0  30     66    136     1
## 12 140       351      0  27     68    120     0

c)

How many missing values are there for gestation, age, height, weight, and smoke, respectively? How many rows of BABIES have no missing values, one missing value, two missing values, and three missing values, respectively? Note: the number of rows in CLEAN should agree with your answer for the number of rows in BABIES that have no missing values. + Rta/:

U1 <- union(which(is.na(BABIES$bwt)),which(is.na(BABIES$gestation)))
U2 <- union(U1,which(is.na(BABIES$parity)))
U3 <- union(U2,which(is.na(BABIES$age)))
U4 <- union(U3,which(is.na(BABIES$height)))
U5 <- union(U4,which(is.na(BABIES$weight)))
U6 <- union(U5,which(is.na(BABIES$smoke)))
Positions <- sort(U6, decreasing = FALSE)

EmptyG <- length(which(is.na(BABIES$gestation)))
EmptyA <- length(which(is.na(BABIES$age)))
EmptyH <- length(which(is.na(BABIES$height)))
EmptyW <- length(which(is.na(BABIES$weight)))
EmptyS <- length(which(is.na(BABIES$smoke)))

print(paste("Missing Values for Gestation: ", EmptyG))
## [1] "Missing Values for Gestation:  13"
print(paste("Missing Values for Age: ", EmptyA))
## [1] "Missing Values for Age:  2"
print(paste("Missing Values for Height: ", EmptyH))
## [1] "Missing Values for Height:  22"
print(paste("Missing Values for Weight: ", EmptyW))
## [1] "Missing Values for Weight:  36"
print(paste("Missing Values for Smoke Status: ", EmptyS))
## [1] "Missing Values for Smoke Status:  10"
lna <- length(Positions)
lb <- length(BABIES$bwt)
lc <- length(CLEAN$bwt)
lcc <- lb-lna
print(paste("Erased Data Length (lna = ",lna,")"))
## [1] "Erased Data Length (lna =  62 )"
print(paste("Total Data Length (lb = ",lb,")"))
## [1] "Total Data Length (lb =  1236 )"
print(paste("CLEAN Length (lc =",lc,")"))
## [1] "CLEAN Length (lc = 1174 )"
print(paste("Subtract Between lb & lna (lcc = ",lcc,")"))
## [1] "Subtract Between lb & lna (lcc =  1174 )"

d)

Use the function complete.cases() to create a “clean” data set that removes subjects if any observations on the subject are “unknown.” Store the modified data frame in a data frame named CLEAN2. Write a line of code that shows all of the values in CLEAN are the same as those in CLEAN2. + Rta/:

CLEAN2 <- complete.cases(BABIES)
CLEAN2_1 <- complete.cases(BABIES)
CLEAN2_1[CLEAN2_1 == "FALSE"] = NA
CLEAN2_2=na.omit(CLEAN2_1)
length(CLEAN2_2)
## [1] 1174
Positions_1 <- which(is.na(CLEAN2_1))
print("Erased Data Position in CLEAN: ")
## [1] "Erased Data Position in CLEAN: "
Positions
##  [1]    4   40   43   86   90   94   99  103  111  114  153  155  159  170  186
## [16]  194  205  219  231  243  255  256  312  338  353  361  364  400  429  433
## [31]  440  444  478  479  482  509  526  601  642  649  651  654  666  672  699
## [46]  707  740  754  763  848  875  880  883  921  964  972 1014 1045 1178 1191
## [61] 1193 1216
print("Erased Data Position in CLEAN2")
## [1] "Erased Data Position in CLEAN2"
Positions_1
##  [1]    4   40   43   86   90   94   99  103  111  114  153  155  159  170  186
## [16]  194  205  219  231  243  255  256  312  338  353  361  364  400  429  433
## [31]  440  444  478  479  482  509  526  601  642  649  651  654  666  672  699
## [46]  707  740  754  763  848  875  880  883  921  964  972 1014 1045 1178 1191
## [61] 1193 1216
print("Subtract Between Vectors")
## [1] "Subtract Between Vectors"
Positions_1 - Positions
##  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [39] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

e)

Sort the values in CLEAN by bwt, gestation, and age. Store the sorted values in a data frame named BGA and show the last six rows. + Rta/:

BWT <- sort(CLEAN$bwt, decreasing = FALSE)
GES <- sort(CLEAN$gestation, decreasing = FALSE)
AGE <- sort(CLEAN$age, decreasing = FALSE) 
BGA <- cbind(BWT, GES, AGE)
tail(BGA,6)
##         BWT  GES   AGE 
## [1169,] "99" "329" "43"
## [1170,] "99" "330" "43"
## [1171,] "99" "330" "43"
## [1172,] "99" "338" "43"
## [1173,] "99" "351" "44"
## [1174,] "99" "353" "45"

f)

Store the data frame CLEAN in your working directory as a _*.csv_ file. + Rta/:

write.csv(CLEAN, file = "C:/Users/flex/Downloads/clean.csv")

g)

What percent of the women in CLEAN are pregnant with their first child (parity = 0) and do not smoke? + Rta/:

PERCENT <- CLEAN[-which(CLEAN$parity == 1),]
PERCENT <- PERCENT[-which(PERCENT$smoke == 1),]
PERCENT <- length(PERCENT$bwt)
TOTAL <- length(CLEAN$parity)
RESULT <- (PERCENT/TOTAL)*100
print(paste(RESULT,"%"))
## [1] "44.7189097103918 %"

1.10

Use the data frame VIT2005 in the PASWR2 package, which contains data on the 218 used apartments sold in Vitoria (Spain) in 2005 to answer the following questions. A description of the variables can be obtained from the help file for this data frame.

library(PASWR2)
## Loading required package: lattice
## Loading required package: ggplot2
library(lattice)
library(ggplot2)
library(optimbase)
## Loading required package: Matrix
library(epiDisplay)
## Loading required package: foreign
## Loading required package: survival
## Loading required package: MASS
## Loading required package: nnet
## 
## Attaching package: 'epiDisplay'
## The following object is masked from 'package:ggplot2':
## 
##     alpha
## The following object is masked from 'package:lattice':
## 
##     dotplot
data(VIT2005)

a)

Create a table of the number of apartments according to the number of garages. + Rta/:

G0 <- VIT2005[-which(VIT2005$garage > 0),]
G1 <- VIT2005[-which(VIT2005$garage != 1),]
G2 <- VIT2005[-which(VIT2005$garage != 2),]
E0 <- VIT2005[which(VIT2005$elevator == 0),]
E1 <- VIT2005[which(VIT2005$elevator == 1),]

APTOS <- c(length(G0[,1]), length(G1[,1]), length(G2[,1]))
names(APTOS) <- c("No Garage","1 Garage","2 Garages")
APTOS
## No Garage  1 Garage 2 Garages 
##       167        49         2

b)

Find the mean of total price according to the number of garages. * Rta/:

NoGaragePrice <- mean(G0$totalprice)
OneGaragePrice <- mean(G1$totalprice)
TwoGaragesPrice <- mean(G2$totalprice)

print(paste("Mean Price With No Garages",NoGaragePrice))
## [1] "Mean Price With No Garages 260537.438502994"
print(paste("Mean Price With One Garages",OneGaragePrice))
## [1] "Mean Price With One Garages 345987.755102041"
print(paste("Mean Price With Two Garages",TwoGaragesPrice))
## [1] "Mean Price With Two Garages 369250"

c)

Create a frequency table of apartments using the categories: number of garages and number of elevators. * Rta/:

AG0 <- length(G0[,1])
AG1 <- length(G1[,1])
AG2 <- length(G2[,1])
TOTALA <- AG0 + AG1 + AG2

RG0 <- AG0/TOTALA
RG1 <- AG1/TOTALA
RG2 <- AG2/TOTALA
TOTALR <- RG0 + RG1 + RG2

AF <- c(AG0, AG1, AG2, TOTALA)
AAF <- c(AG0, AG1 + AG0, AG2 + AG1 + AG0, TOTALA)
RF <- c(RG0, RG1, RG2, TOTALR)
ARF <- c(RG0, RG1 + RG0, RG2 + RG1 + RG0, TOTALR)

FT <- matrix(c(AF , AAF , RF , ARF) ,nrow = 4, ncol = 4)
colnames(FT) <- c("Abs. Freq.","Accum. Abs. Freq.","Rel. Freq", "Accum. Rel. Freq")
rownames(FT) <- c(0,1,2,"Total")
First <- tab1(VIT2005$garage, sort.group = "decreasing", cum.percent = TRUE)

FT
##       Abs. Freq. Accum. Abs. Freq.   Rel. Freq Accum. Rel. Freq
## 0            167               167 0.766055046        0.7660550
## 1             49               216 0.224770642        0.9908257
## 2              2               218 0.009174312        1.0000000
## Total        218               218 1.000000000        1.0000000
First
## VIT2005$garage : 
##         Frequency Percent Cum. percent
## 0             167    76.6         76.6
## 1              49    22.5         99.1
## 2               2     0.9        100.0
##   Total       218   100.0        100.0
#########################################################################################
AE0 <- length(E0[,1])
AE1 <- length(E1[,1])
ETOTALA <- AE0 + AE1

RE0 <- AE0/ETOTALA
RE1 <- AE1/ETOTALA
ETOTALR <- RE0 + RE1

AFE <- c(AE0, AE1, ETOTALA)
AAFE <- c(AE0, AE1 + AE0, ETOTALA)
RFE <- c(RE0, RE1, ETOTALR)
ARFE <- c(RE0, RE1 + RE0, ETOTALR)

FTE <- matrix(c(AFE , AAFE , RFE , ARFE) ,nrow = 3, ncol = 4)
colnames(FTE) <- c("Abs. Freq.","Accum. Abs. Freq.","Rel. Freq", "Accum. Rel. Freq")
rownames(FTE) <- c(0,1,"Total")
Second <- tab1(VIT2005$elevator, sort.group = "decreasing", cum.percent = TRUE)

FTE
##       Abs. Freq. Accum. Abs. Freq. Rel. Freq Accum. Rel. Freq
## 0             44                44 0.2018349        0.2018349
## 1            174               218 0.7981651        1.0000000
## Total        218               218 1.0000000        1.0000000
Second
## VIT2005$elevator : 
##         Frequency Percent Cum. percent
## 1             174    79.8         79.8
## 0              44    20.2        100.0
##   Total       218   100.0        100.0

d)

Find the mean flat price (total price) for each of the cells of the table created in part (c). * Rta/:

NoGaragePrice
## [1] 260537.4
OneGaragePrice
## [1] 345987.8
TwoGaragesPrice
## [1] 369250
NoElevatorPrice <- mean(E0$totalprice)
OneElevatorPrice <- mean(E1$totalprice)
NoElevatorPrice #Precio de apto sin elevador
## [1] 210492.1
OneElevatorPrice #Precio de apto con un elevador
## [1] 298505.7

e)

What command will select only the apartments having at least one garage? * Rta/:

G_12 <- VIT2005[which(VIT2005$garage > 0),]
G_12
##     totalprice   area zone category age floor rooms  out conservation toilets
## 2       409000 100.65  Z31       3B   5     7     5  E50           1A       2
## 13      560000 155.90  Z21       2B   7     4     6 E100           1A       2
## 20      360500 108.74  Z43       3A  14     3     5  E75           1A       2
## 22      380000 103.00  Z37       3B  15     3     6  E50           1A       1
## 24      372000 115.59  Z44       3B  17     4     6 E100           1A       2
## 27      330000  94.53  Z56       2B   7     3     4 E100           1A       2
## 33      333000  91.89  Z41       3B  14     5     5 E100           1A       2
## 34      403000 118.86  Z41       3A  25     5     6 E100           1A       2
## 37      330500 110.71  Z61       3A  18     5     5 E100           1A       2
## 39      373000 103.72  Z37       3A  29     3     5  E50           1A       2
## 48      426200 141.48  Z42       3A  13     5     5 E100           1A       2
## 50      326000  86.86  Z41       3B  15     4     5 E100           1A       2
## 54      308000 104.27  Z52       3A   7     2     5  E50           1A       2
## 55      345000 114.78  Z31       3B  16     4     5  E50           1A       2
## 56      390000  90.92  Z41       2A  10     3     5 E100           1A       2
## 58      343000 115.59  Z44       3B  16     5     6 E100           1A       2
## 64      329000  92.94  Z56       3A   7     3     5 E100           1A       2
## 67      291500  69.18  Z56       2B   9     7     4 E100           1A       1
## 69      331600  96.59  Z52       3A   8     2     5 E100           1A       2
## 73      294000 111.37  Z53       3B  19    10     5 E100           2B       2
## 75      347500  93.71  Z56       3A   8     4     5 E100           1A       2
## 78      286000 124.45  Z52       3A  24     5     7  E50           1A       2
## 79      325000 105.58  Z56       3A   4     2     5 E100           1A       2
## 83      379500 109.20  Z42       3A  13     5     5 E100           1A       2
## 90      327500 106.15  Z56       2A   5     4     5  E50           1A       2
## 97      392500 118.68  Z41       3A  15     2     5 E100           1A       2
## 99      262000  88.44  Z47       3B  18     5     5 E100           1A       2
## 103     398000  92.21  Z41       3A  10     2     5 E100           1A       2
## 104     258000  81.00  Z52       3B  15     8     5 E100           1A       1
## 107     340000 103.51  Z41       3A  12     5     5 E100           1A       2
## 109     273000  74.79  Z45       3B  10     4     4  E50           1A       1
## 114     370000 115.34  Z56       2A   6     4     5  E50           1A       2
## 130     378000 104.07  Z42       3A  19     3     5 E100           1A       2
## 131     347000 106.60  Z36       3A  14     7     6 E100           1A       2
## 132     240000  71.05  Z62       3A  14     3     4 E100           1A       1
## 133     457000 142.18  Z42       3A  14     4     6 E100           2A       2
## 135     348000 112.47  Z43       3A  26     2     5  E50           1A       2
## 136     391000  90.92  Z41       2B  10     5     5 E100           1A       2
## 138     398000  87.44  Z41       3A  11     2     5 E100           1A       2
## 140     257500  80.34  Z45       3B  23     6     5  E50           2B       2
## 141     341000  95.84  Z43       3B  27     3     5 E100           1A       2
## 152     396000 114.91  Z42       3A  11     7     5  E50           1A       2
## 154     303500  91.93  Z61       2B  13     4     5  E50           1A       2
## 155     354600 100.64  Z36       2B  22     6     5 E100           1A       2
## 186     433500 113.51  Z41       2B   8     4     5 E100           1A       2
## 190     366000 106.60  Z36       3B  19     2     6 E100           1A       2
## 192     320000  98.28  Z61       3B  22     5     5 E100           1A       2
## 199     261500  96.33  Z47       4A  17     5     4 E100           1A       2
## 207     285000  97.16  Z41       3A  13     2     5 E100           1A       2
## 215     340000  95.61  Z56       2B   8     6     5  E50           1A       2
## 216     280000  83.22  Z34       3B  29     7     5  E50           2B       1
##     garage elevator streetcategory heating storage
## 2        1        1             S5      4A       1
## 13       1        1             S4      3B       1
## 20       1        1             S3      4A       1
## 22       1        1             S3      3A       1
## 24       1        1             S4      3A       1
## 27       1        1             S3      3A       1
## 33       1        1             S4      4A       1
## 34       1        1             S4      4A       1
## 37       1        1             S3      4A       1
## 39       1        1             S2      4A       1
## 48       1        1             S5      3A       1
## 50       1        1             S2      3A       1
## 54       1        1             S3      3A       1
## 55       1        1             S5      3A       1
## 56       1        1             S4      3B       1
## 58       1        1             S4      3A       1
## 64       1        1             S2      3A       1
## 67       1        1             S3      3A       1
## 69       1        1             S3      3A       2
## 73       1        1             S4      3A       1
## 75       2        1             S3      3A       1
## 78       1        1             S2      3A       0
## 79       1        1             S3      3A       0
## 83       1        1             S4      3A       1
## 90       1        1             S3      3B       1
## 97       1        1             S4      3A       1
## 99       1        1             S2      3A       1
## 103      1        1             S4      4A       1
## 104      1        1             S2      4A       1
## 107      1        1             S2      3A       1
## 109      1        1             S3      4A       1
## 114      1        1             S3      3B       1
## 130      1        1             S4      3A       1
## 131      1        1             S2      3A       1
## 132      1        1             S3      3A       0
## 133      1        1             S4      3A       1
## 135      1        1             S4      3B       1
## 136      2        1             S4      3A       0
## 138      1        1             S4      4A       1
## 140      1        1             S4      3A       1
## 141      1        1             S4      4A       1
## 152      1        1             S5      3A       1
## 154      1        1             S3      3A       1
## 155      1        1             S2      3A       1
## 186      1        1             S4      3B       1
## 190      1        1             S2      3A       1
## 192      1        1             S3      3A       1
## 199      1        1             S3      4A       1
## 207      1        1             S2      3A       1
## 215      1        1             S3      3A       1
## 216      1        1             S3      4A       1

f)

Define a new file called data.c with the apartments that have category = “3B” and have an elevator. * Rta/:

C_3B <- VIT2005[which(VIT2005$category == "3B"),]
data.c <- c(C_3B, E1)

1.11

Use the data frame EPIDURALF to answer the following questions:

library(PASWR2)
data("EPIDURALF")
EL <- length(EPIDURALF$doctor)

a)

How many patients have been treated with the Hamstring Stretch? * Rta/:

HS <- EPIDURALF[-which(EPIDURALF$treatment != "Hamstring Stretch"),]
HSL <- length(HS$treatment)
HSL
## [1] 171

b)

What percent of the patients treated with Hamstring Stretch were classified as each of Easy, Difficult, and Impossible? * Rta/:

HSE <- HS[-which(HS$ease != "Easy"),]
HSD <- HS[-which(HS$ease != "Difficult"),]
HSI <- HS[-which(HS$ease != "Impossible"),]

HSEL <- length(HSE$ease)
HSDL <- length(HSD$ease)
HSIL <- length(HSI$ease)

print(paste("Percent of Patients Classified as Easy in HS treatement: ",PE <- (HSEL/HSL)*100," %"))
## [1] "Percent of Patients Classified as Easy in HS treatement:  58.4795321637427  %"
print(paste("Percent of Patients Classified as Difficult in HS treatement: ",PD <- (HSDL/HSL)*100," %"))
## [1] "Percent of Patients Classified as Difficult in HS treatement:  36.8421052631579  %"
print(paste("Percent of Patients Classified as Difficult in HS treatement: ",PI <- (HSIL/HSL)*100," %"))
## [1] "Percent of Patients Classified as Difficult in HS treatement:  4.67836257309941  %"
#PE + PD + PI

PE_1 <- (HSEL/EL)*100
PD_1 <- (HSDL/EL)*100
PI_1 <- (HSIL/EL)*100

P_1 <- c(PE_1, PD_1, PI_1)
names(P_1) <- c("% Easy","% Difficult","% Impossible")
P_1
##       % Easy  % Difficult % Impossible 
##    29.239766    18.421053     2.339181

c)

What percent of the patients classified as Easy to palpate were assigned to the Traditional Sitting position? * Rta/:

EE <- EPIDURALF[-which(EPIDURALF$ease != "Easy"),]
TSE_1 <- EE[-which(EE$treatment != "Traditional Sitting"),]

EEL <- length(EE$ease)
TSEL <- length(TSE_1$treatment)

P_2<-(TSEL/EEL)*100
print(paste(P_2," %"))
## [1] "51.6908212560386  %"

d)

What is the mean weight for each cell in a contingency table created with the variables Ease and Treatment? + Rta/:

TS <- EPIDURALF[-which(EPIDURALF$treatment != "Traditional Sitting"),]

TSE <- TS[-which(TS$ease != "Easy"),]
TSD <- TS[-which(TS$ease != "Difficult"),]
TSI <- TS[-which(TS$ease != "Impossible"),]

TSEL <- length(TSE$ease)
TSDL <- length(TSD$ease)
TSIL <- length(TSI$ease)

TOTAL_TS <- TSEL + TSDL + TSIL
TOTAL_HS <- HSEL + HSDL + HSIL

TOTAL_E <- TSEL + HSEL
TOTAL_D <- TSDL + HSDL
TOTAL_I <- TSIL + HSIL

TOTAL <- TOTAL_E + TOTAL_D + TOTAL_I

CT <- matrix(c(TSEL, HSEL, TOTAL_E, TSDL, HSDL, TOTAL_D, TSIL,  HSIL, TOTAL_E, TOTAL_TS, TOTAL_HS, TOTAL) ,nrow = 3, ncol = 4)
colnames(CT) <- c("Easy","Difficult","Impossible", "Total")
rownames(CT) <- c("Traditional","Hamstring","Total")
CT
##             Easy Difficult Impossible Total
## Traditional  107        51         13   171
## Hamstring    100        63          8   171
## Total        207       114        207   342
M1_1 <- mean(TSE$kg)
M1_2 <- mean(TSD$kg)
M1_3 <- mean(TSI$kg)
M1_4 <- mean(TS$kg)

M2_1 <- mean(HSE$kg)
M2_2 <- mean(HSD$kg)
M2_3 <- mean(HSI$kg)
M2_4 <- mean(HS$kg)

print(paste("Mean Weights from each column of row 1: ",M1_1," kg, ",M1_2," kg, ",M1_3," kg, ",M1_4," kg"))
## [1] "Mean Weights from each column of row 1:  79.4018691588785  kg,  94.2745098039216  kg,  113.615384615385  kg,  86.4385964912281  kg"
print(paste("Mean Weights from each column of row 2: ",M2_1," kg, ",M2_2," kg, ",M2_3," kg, ",M2_4," kg"))
## [1] "Mean Weights from each column of row 2:  78.67  kg,  92.6666666666667  kg,  127.875  kg,  86.1286549707602  kg"

e)

What percent of the patients have a body mass index (\(BMI= kg/(cm/100^2\))) less than 25 and are classified as Easy to palpate? * Rta/:

BMI <- EE$kg/(EE$cm/100)^2
CLASS <- EE[which(BMI < 25),]
CL <- length(CLASS$doctor)

print(paste((CL/EL)*100," %"))
## [1] "9.06432748538012  %"

1.12

The millions of tourists visiting Spain in 2003, 2004, and 2005 according their nationalities are given in the following table:

a)

Store the values in this table in a matrix with the name tourists.

library(PASWR2)
  • Rta/:
colum_1 <- c("German", "French", "British", "American", "Rest of the world")
Fila_1<-c("2003","2004","2005")
datos<-c(9.303,9.536,9.918,7.959,7.736,8.875,15.224,15.629,16.090,0.905,0.894,0.883,17.463,18.635,20.148)
tourists <- matrix(data=datos,nrow=5,byrow=TRUE)
tourists #tourists.
##        [,1]   [,2]   [,3]
## [1,]  9.303  9.536  9.918
## [2,]  7.959  7.736  8.875
## [3,] 15.224 15.629 16.090
## [4,]  0.905  0.894  0.883
## [5,] 17.463 18.635 20.148
dimnames(tourists)<-list(colum_1,Fila_1)
tourists
##                     2003   2004   2005
## German             9.303  9.536  9.918
## French             7.959  7.736  8.875
## British           15.224 15.629 16.090
## American           0.905  0.894  0.883
## Rest of the world 17.463 18.635 20.148
is.matrix(tourists)
## [1] TRUE

b)

Calculate the totals of the rows. + Rta/:

apply(X=tourists, MARGIN=1,FUN=sum)#Suma de las filas: Total de turistas por Nacionalidad (Por millon)
##            German            French           British          American 
##            28.757            24.570            46.943             2.682 
## Rest of the world 
##            56.246

c)

Calculate the totals of the columns. + Rta/:

apply(X=tourists, MARGIN=2,FUN=sum)#Suma de las columnas: Total de turistas por año (Por millon)
##   2003   2004   2005 
## 50.854 52.430 55.914