-
Notifications
You must be signed in to change notification settings - Fork 0
Day 4 recap
Meghan Balk edited this page Apr 6, 2018
·
1 revision
Day 4 exercises
### Loops
#first loop!
for(time in 2:4){
print(paste("I have lived in DC for", time, "years"))
}
x <- c(2:4) # try different values for x
for(time in x){
print(paste("I have lived in DC for", time, "years"))
}
# use words
x <- c("two", "three", "four")
for(time in x){
print(paste("I have lived in DC for", time, "years"))
}
# use negative numbers
x <- c(-4:-2)
for(time in x){
print(paste("I have lived in DC for", time, "years"))
}
# insert iris
x <- iris
for(time in x){
print(paste("I have lived in DC for", time, "years"))
}
species <- letters[1:4]
local.1 <- rep(1, 4)
local.2 <- c(0, 0, 1, 0)
local.3 <- c(0, 0, 1, 1)
local.4 <- c(0, 1, 1, 0)
occ <- data.frame(local.1, local.2, local.3, local.4)
rownames(occ) <- species
occ
#sum of sp a occ
sum(occ[1,])
#create a loop to get the sum for all spp occ
for(i in 1:nrow(occ)){
print(sum(occ[i,]))
}
rowSums(occ)
for (i in 1:length(species)) {
if (sum(occ[i,]) == 1){
next #ends reading the loop & goes to the next [i] in the sequence
}
print(sum(occ[i,]))
}
# pull up the iris dataset. Get column sums for all of them from things greater than 3.
iris
for(i in 1:ncol(iris)){ #range of columns
x <- iris[, i] #pull out a single column
if(!(is.numeric(x))){
next
}
greater.than.3 <- x[x > 3] #pull out values within column that are greater than 3
print(sum(greater.than.3)) #get a sum of those values in each column that are greater than 3
}
##While
i = 2 # initial value
while(i < 1000) {
print(i)
i = i^2 #squares i each iteration
}
#endless hippos
i = 2 # initial value
while(i > 1) {
print("hippo")
i = i^2 #squares i each iteration
}
#nested loops
#use a loop to get each cell:
for(i in 1:length(occ)){ # for each column...
#print(sum(occ[i, ]))
for(j in 1:length(species)){ # for each row...
print(occ[j, i])
}
}
printName <- function(name = "Joe Shmoe"){
print(name)
print("That's my name.")
}
printName("Lucy Goosey")
square.it <- function(x) { #x is the argument
square <- x * x #square is the object
return(square) #return the object
}
square.it(-4)
square.it(256)
square.it("cat")
square.it(c(1, 4, 2))
matrix1 <- cbind(c(3, 10), c(4, 5))
matrix1
square.it(matrix1)
my.second.fun <- function(matrix, vector) {
if (dim(matrix)[2] != length(vector)) {
stop("Can't multiply matrix%*%vector because the dimensions are wrong")
}
product <- matrix %*% vector
return(product)
}
#turn this for loop into a function
for(i in 1:length(occ)){
print(sum(occ[i, ])) # row
}
sum.function1 <- function(matrix){
for(i in 1:nrow(matrix)){
print(sum(matrix[i, ])) # row
}
}
sum.function2 <- function(df){
x <- rowSums(df)
return(x)
}
?plot
plot(Sepal.Length ~ Sepal.Width, data = iris)
#plot with triangles and a title
plot(Sepal.Length ~ Sepal.Width, data = iris,
pch = 2,
main = "Behold My Data")
#add a line
model.iris <- lm(Sepal.Length ~ Sepal.Width, data = iris)
abline(model.iris)
#make the line dashed
abline(model.iris, lty = 2)
pairs(iris)
#histogram
hist(iris$Petal.Length)
#log-transformed
hist(log(iris$Petal.Length))
#box plot
boxplot(Petal.Width ~ Species, data = iris)
#reorder species
iris$Species <- factor(iris$Species, levels = c("virginica", "versicolor", "setosa"))
boxplot(Petal.Width ~ Species, data = iris)
Day 4 practice problems
#Get the minimum value, maximum value, range of values, and median value of petal widths for each species.
#functions: min(), max(), range(), median()
min.petal.w <- c()
max.petal.w <- c()
spread.petal.w <- c()
median.petal.w <- c()
species <- as.character(unique(iris$Species)) #counter
for(sp in 1:length(species)){ #goes through each element
min.petal.w <- c(min.petal.w, min(iris$Petal.Width[iris$Species == species[sp]]))
#print(sp)
#print(min.petal.w)
max.petal.w <- c(max.petal.w, max(iris$Petal.Width[iris$Species == species[sp]]))
spread.petal.w <- c(spread.petal.w, max.petal.w - min.petal.w)
median.petal.w <- c(median.petal.w, median(iris$Petal.Width[iris$Species == species[sp]]))
}
for(sp in species){ #goes through each element
min.petal.w <- min(iris$Petal.Width[iris$Species == sp])
max.petal.w <- max(iris$Petal.Width[iris$Species == sp])
spread.petal.w <- max.petal.w - min.petal.w
median.petal.w <- median(iris$Petal.Width[iris$Species == sp])
print(c(min.petal.w, max.petal.w, spread.petal.w, median.petal.w))
}
char <- c("a", "b", "c")
store.it <- c()
for(i in c(1, 2, 3)){
print(char[i])
store.it <- c(store.it, paste(char[i], "added"))
}
store.it
store.it <- c()
for(i in c("a", "b", "c")){
print(i)
store.it <- c(store.it, paste(i, "added"))
}
store.it
let <- c()
for(i in 1:4){
let <- letters[i]
}
let
# equivalent to:
let <- c()
let <- letters[1]
let <- letters[2]
let <- letters[3]
let <- letters[4]
let
let <- c()
let <- c(let, letters[1])
let <- c(let, letters[2])
let <- c(let, letters[3])
let <- c(let, letters[4])
let
Day 1 or 2
- Basics of R
- Variables and objects
- Functions and packages
- Conditionals and logical operators
- Practice
- Recap
Day 3
Day 4
Day 5
Extra