Source file ⇒ Lab_8.Rmd

Question 1

##  [1]    1    4    9   16   25   36   49   64   81  100  121  144  169  196
## [15]  225  256  289  324  361  400  441  484  529  576  625  676  729  784
## [29]  841  900  961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681 1764
## [43] 1849 1936 2025 2116 2209 2304 2401 2500

Question 2

  1. 0, 2, 4, 6, 8
  2. NA, NA, 4, 6, 8
  3. 0, 0, 0, 0, 0
  4. 0

Question 3 1) The lapply function in R operates on a list or vector and returns a list.

Question 4

set.seed(1337)
m <- matrix(runif(15000, -3, 3), ncol = 3)

loop_function <- function(m) {
a <- c()
for (i in 1:5000){
  z <- sum(m[i,]^2)
  a[i] <- z
}
m.ssq.loop <- a
}

head(loop_function(m))
## [1] 10.219360  4.811994  9.842371  7.161204  6.058554 14.288626
m.ssq.apply <- apply(m, 1,function(x) {sum(x^2)})
head(m)
##            [,1]       [,2]       [,3]
## [1,]  0.4579293 -1.5301484  2.7691707
## [2,]  0.3884528  1.1452789 -1.8301459
## [3,] -2.5560586  1.3678571 -1.1991257
## [4,] -0.2768063 -2.5663416 -0.7060263
## [5,] -0.7603244 -0.5228916 -2.2818951
## [6,] -1.0120953 -2.1568870  2.9346426
head(loop_function(m))
## [1] 10.219360  4.811994  9.842371  7.161204  6.058554 14.288626
head(m.ssq.apply)
## [1] 10.219360  4.811994  9.842371  7.161204  6.058554 14.288626
sanity_check <- function(x,y) {
  if(x == y){
    print("Correct")
} else{
    print("Not Correct")}
}

sanity_check(x = 1, y = 1)
## [1] "Correct"

Question 5

Year <- c(2000, 2001)
Algeria <- c(7 ,9)
Brazil <- c(12, NA)
Columbia <- c(16, 18)


table1 <- data.frame(Year, Algeria, Brazil, Columbia)
table_2 <- table1 %>% gather(Country, Value, Algeria, Brazil, Columbia)
table_2
##   Year  Country Value
## 1 2000  Algeria     7
## 2 2001  Algeria     9
## 3 2000   Brazil    12
## 4 2001   Brazil    NA
## 5 2000 Columbia    16
## 6 2001 Columbia    18
table_3 <- table_2 %>% group_by(Year)  %>% mutate(Average=mean(Value, na.rm = TRUE)) %>% select(Year, Country, Average, Value) %>% spread(Country, Value)
table_3
## Source: local data frame [2 x 5]
## Groups: Year [2]
## 
##    Year  Average Algeria Brazil Columbia
##   (dbl)    (dbl)   (dbl)  (dbl)    (dbl)
## 1  2000 11.66667       7     12       16
## 2  2001 13.50000       9     NA       18

Question 6 a) The glyphs are the linear regression lines and the colored dots. b) Education and Wage are represented by the dots and Sex is determined by color. c) Education is mapped to the x-axis, Wage is mapped to the y-axis, and Sex is mapped to color. d) Wage and Education are quantitative while sex is Qualitative. e) There are none save for the color guide to the right. f)

data_table <- CPS85
graph <- ggplot(data_table, aes(x = educ, y = wage, col = sex)) + geom_point(size = 3) + geom_smooth(method = "lm") +  labs(title = "Wage vs. Education in CPS85", x = "Education", y = "Wage") + theme(plot.title = element_text(size = 20))
graph

Question 7

my.string <- "ggplot2 is a data visualization package for the statistical programming language R"

SplitChars <- function(string) {
  new_string <- strsplit(string, "")
}

load_new_string <- unlist(SplitChars(my.string))
load_new_string
##  [1] "g" "g" "p" "l" "o" "t" "2" " " "i" "s" " " "a" " " "d" "a" "t" "a"
## [18] " " "v" "i" "s" "u" "a" "l" "i" "z" "a" "t" "i" "o" "n" " " "p" "a"
## [35] "c" "k" "a" "g" "e" " " "f" "o" "r" " " "t" "h" "e" " " "s" "t" "a"
## [52] "t" "i" "s" "t" "i" "c" "a" "l" " " "p" "r" "o" "g" "r" "a" "m" "m"
## [69] "i" "n" "g" " " "l" "a" "n" "g" "u" "a" "g" "e" " " "R"
count <- 0

for (i in 1:length(load_new_string)) {
  if(load_new_string[i] == "r" | load_new_string[i] == "R" | load_new_string[i] == "a" | load_new_string[i] == "s" ){
    count <- count + 1}
}
print(count)
## [1] 20
Reverse <- function(string) {
  split_string <- unlist(SplitChars(string))
  reverse <- rev(split_string)
  print(reverse)
}
Reverse(my.string)
##  [1] "R" " " "e" "g" "a" "u" "g" "n" "a" "l" " " "g" "n" "i" "m" "m" "a"
## [18] "r" "g" "o" "r" "p" " " "l" "a" "c" "i" "t" "s" "i" "t" "a" "t" "s"
## [35] " " "e" "h" "t" " " "r" "o" "f" " " "e" "g" "a" "k" "c" "a" "p" " "
## [52] "n" "o" "i" "t" "a" "z" "i" "l" "a" "u" "s" "i" "v" " " "a" "t" "a"
## [69] "d" " " "a" " " "s" "i" " " "2" "t" "o" "l" "p" "g" "g"