STA 255

1.) Create the vector serial with name serial using

  1. c() funtion
serial <- c(1,2,3,4,5,6,7,8,9,10,11,12)
  1. using a:b function
serial <- c(1:12)
  1. using seq() function
serial <- seq(1, 12, by =1)

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") 

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

# I could not find another way to create the vector with those values.

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 eyes?

sum(sex == "male" & eye_color == "blue") 
## [1] 2

7.) For example, in another experiment, the psychologist is looking for male patients with brown eye and female patients 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(before < after)
## [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” and “lwd” c) Use different colors to distinguish two graphs.

plot(serial, before, type = "o", lty = 5, lwd = 1.5, pch = "o", cex = 0.75, col = "blue", xlab = "Serial", ylab = "Cognitive Test Results", ylim = c(0,100))
lines(serial, after, type = "o", pch = "x", cex= 0.8, col = "red", lty = 1, lwd = 1) 

# after results have a red solid line
# before results have a blue dashed line

10.) Create a figure with 3 graphs in a row: one with the array before, next with after and third with a difference bewteen before and after. The third graph is related to problem 8.

plot(serial, before, type = "o", col = "blue")

plot(serial, after, type = "o", col = "red")

plot(serial, abs(before-after), ylab = "Difference between before and after",type = "o", col = "purple")