Commit 14dc2448 authored by Sarah Blum's avatar Sarah Blum

added solutions to all exercise sheets so far

parent 9edcbf69
......@@ -2,3 +2,5 @@
.Rhistory
.RData
.Ruserdata
packrat/lib*/
packrat/src/
#### 1) Gather information about a person in a list
addr_list <- list(street = "Mainstreet", zip_code = 12345, town = "NYC")
grades <- c(1,2,1,2,1,2,1)
info <- list(age = 30,
age_days = 10950,
weekday = "Tuesday",
address = addr_list,
university_grades = grades,
height = 1.6,
location = c(40.7128, 74.0060),
mail = "person@info.com")
#### 2) I AM SORRY, I DONT UNDERSTAND THE TASK
# calculate relative risk for lower birthweight TODO this is not correct.
library(MASS)
data("birthwt")
calc_rr_low_bt <- function(dataset){
# create frequencies
a <- c(sum(dataset$smoke == 1 & dataset$low == 1))
b <- c(sum(dataset$smoke == 0 & dataset$low == 1))
c <- c(sum(dataset$smoke == 1 & dataset$low == 0))
d <- c(sum(dataset$smoke == 0 & dataset$low == 0))
# this is not correct
return((a / (a + c)) / (b / (b + d)))
}
##### 3) fibonacci number variations
# for loop
len <- 12
fibvals <- numeric(len)
fibvals[1] <- 1
fibvals[2] <- 1
for (i in 3:len) {
fibvals[i] <- fibvals[i - 1] + fibvals[i - 2]
}
print(fibvals)
# while loop
a <- 0
b <- 1
counter <- 0
while (counter < 12) {
counter <- counter + 1
print(b)
temp <- a + b
a <- b
b <- temp
}
# repeat
n <- 3
len <- 12
fib <- numeric(len)
fib[1] <- 1
fib[2] <- 1
repeat {
fib[n] <- fib[n - 1] + fib[n - 2]
n <- n + 1
if (n > len) {
break
}
}
print(fib)
#### Exercise Functions
# 1) function for sum of two integers:
# usage: sum_int(as.integer(4), as.integer(6))
sum_int <- function(a,b) {
stopifnot(is.integer(a) || is.integer(b))
return(a + b)
}
# 2) boolean function is in vector
is_in_vec <- function(a, v) {
stopifnot(is.vector(v))
return(a %in% v)
}
# 3) dataframe info
dataframe_info <- function(df, column) {
stopifnot (is.data.frame(df))
if (column > length(df)) {
stop("Dataframe does not have that many columns")
}
type <- class(df[,column])
nam <- colnames(df)[column]
print(paste(nam , " is " , type))
}
# 4) create unique which removes duplicates
my_unique <- function(v) {
stopifnot (is.vector(v))
return(unique(v))
}
# 5) count occurences
count_occurences <- function(v, a) {
stopifnot (is.vector(v) & is.integer(a))
return(sum(v == a))
}
# 6) descriptive stats
descriptives <- function(v, printMedian=FALSE) {
print(mean(v))
print(sd(v))
if (printMedian) {
print(median(v))
}
}
# 7) how many devisiors
which_devisors <- function(a) {
y <- seq_len(a)
# if modulo operation is zero, we have found a divisor
# put it into y
y[ a %% y == 0 ]
}
# 8) student plot
student_plot <- function(marks, names) {
plot(marks,xaxt = "n") # plot without xticks first
axis(1, at = 1:length(marks), labels = names)
}
# 1
library(ggplot2)
# qplot(x, y, data=, color=, shape=, size=, alpha=, geom=, method=, formula=, facets=, xlim=, ylim=
# xlab=, ylab=, main=, sub=)
View(diamonds)
qplot(diamonds$carat[1:50], diamonds$price[1:50], color = diamonds$color[1:50])
qp <- qplot(x = diamonds$carat[1:50],
y = diamonds$price[1:50],
color = diamonds$color[1:50])
qp + facet_grid(diamonds$cut ~ .)
# 2
# solution with qplot
qplot(carat, data = diamonds, geom = "density", colour = color)
set.seed(123)
dsample = diamonds[sample(nrow(diamonds),100), ]
ggplot(diamonds, aes(x = carat, color = color)) +
geom_density()
# if we add brackets, this is printed out in the console
y <- 5
(y <- 5)
vector[-1] # gives every element but the first one
m <- matrix(c(1:12), nrow = 3, ncol = 4, byrow = TRUE)
# tertiary operator
# ifelse(conditon, content1, content2)
#
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment