Homework 1

By Cade Corcoran

1 Create the vector serial with name serial using a) c() function b) using a:b function c) using seq() function

serial<- c(1,2,3,4,5,6,7,8,9,10,11,12)
serial <- (1:12)
serial <- seq(1,12)

2 Now use c() function to create the following vectors: a) before b) after c) age d) sex e) eye_color

before<- c(45,49, 40,48,44, 70,90,75,80, 65,80,52)
after<- c(43,50,61, 44,45,20, 85,65,72, 65,70,75)
age<- c(20, 19,22,20, 27,22,22,20,25,22,24,22)
sex<- c("Male", "Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male", 
        "Female", "Male", "Female")
eye_color<- c("Blue", "Blue", "Brown", "Brown", "Blue", 
              "Blue", "Brown", "Brown", "Blue", "Blue", "Brown", "Brown")
Table<- data.frame(serial, before, after, age, sex, eye_color)
data(Table)
## Warning in data(Table): data set 'Table' not found
str(Table)
## 'data.frame':    12 obs. of  6 variables:
##  $ serial   : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ before   : num  45 49 40 48 44 70 90 75 80 65 ...
##  $ after    : num  43 50 61 44 45 20 85 65 72 65 ...
##  $ age      : num  20 19 22 20 27 22 22 20 25 22 ...
##  $ sex      : Factor w/ 2 levels "Female","Male": 2 1 2 1 2 1 2 1 2 1 ...
##  $ eye_color: Factor w/ 2 levels "Blue","Brown": 1 1 2 2 1 1 2 2 1 1 ...

3 Can you create the vectors sex and eye_color using a function different from the function c()? No

4 How many of the patients are above 20 years old?

sum(age>20)
## [1] 8

5 What is the median age of the patients?

median(age)
## [1] 22

6 How many male patients have blue eye?

sum(sex=="Male" & eye_color=="Blue")
## [1] 3

7 For example, in another experiment, the psychologist is looking for male patients with brown eye and female patient with blue eye from the same data set. What is the sample size of his/her next experiment?

sum(sex=="Male" & eye_color=="Brown" | sex=="Female" & eye_color=="Blue")
## [1] 6

8 How many patients’ results shows better score (improvement) after the experiment?

sum(after>before)
## [1] 4

9 Represent two curves with the results before and after in the same graph. Use a) Different symbol with “pch” and use “cex” to increase the dimension of the symbol. b) Different line types with “lty”, “lwd” c) Use different colors to distinguish two graphs.

plot(serial, before, type="o", lty=6, pch = "*", xlab="Serial", 
     ylab="Before/After", ylim=c(0,100))
points(serial, after, col="red", pch ="+")
lines(serial, after, col="red", lty=2)

10 Create a figure with 3 graphs in a row: one with the array “before”, next with “after” and third one with a difference between before and after. Note that the third graph is related to the problem #8.

plot(serial, before, type="o", lty=6, col="orange")                                                

plot(serial, after, type="o", lty=6, col="black")

plot(serial, before, type="o", lty=6, pch = "*", xlab="Serial", 
     ylab="Before/After", ylim=c(0,100))
points(serial, after, col="red", pch ="+")
lines(serial, after, col="red", lty=2)

par(mfrow=c(1,3))