Skip to content

Day 1 or 2 recap

Meghan Balk edited this page Apr 4, 2018 · 2 revisions

Day 1 or 2 exercises

#this file is for day 1

2+2 #can R still do the arithmetic?

grape <- "red"
"red" -> grape
grape

grape <- red

grape <- "green"

grape <- 2 
grape

grape*5
grape^2

ten.grapes <- grape*5
ten.grapes

class(ten.grapes)

2+2
2 + "2"

is.numeric(2)
is.numeric("2")
class(is.numeric("2"))

x <- c(1:5)
x
y <- c(1.1, 2.2, 3.3, 4.4, 5.5)
y
class(x)
class(y)
z <- as.integer(y)

a <- c(x, y)
a

green.grapes <- "green"
grape
all.grapes <- c(green.grapes, grape)

class(grape)
grapeChar <- as.character(grape)
grapeChar*10


univ <- c(0, 0, 1, 0, 1)
univ
is.factor(univ)
is.numeric(univ)

univFactor <- factor(univ, labels = c("Private", "Public"))
is.factor(univFactor)

univFactor

x = 3
x <- 3

grape > 2
grape >= 2

grape == "red"
grape == "red" | 2
grape == "red" & 2


a <- c(3, 6, 15, 20)
a
class(a)
a * 3
a > 5
a <- c(a, "thing")
class(a)
a * 3

ten <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
ten[3]

iris[2, 3]

stuff <- list(c(66:101),"second",mtcars)
stuff[[2]]

stuff[[1]][18]
stuff[[3]][4, 3]

stuff[[1]] < 70 | stuff[[1]] > 80


log(x = 3, base = 10)
a <- log(x = 3, base = 10)
a

install.packages("dplyr")
install.packages(c("ggplot2", "tidyr"))

library(dplyr)
library(ggplot2)
library(tidyr)

?log
help(log)
example(log)

log(x = 3, base = 10)
log(3, 10)
log(10, x = 3)

?seq
every.other <- seq(from = 1.1, to = 4.6, by = 0.5)
every.other
every.other <- c(every.other, "apples")
every.other

every.other <- c(seq(1.1, 4.6, 0.5), "apples")
every.other

getwd()
setwd("/Users/meghanbalk/Desktop")
getwd()

print("Start run of code.")

if(TRUE){
  ### any code you want can be here ###
  print("Our 'if' statement is true.")
}

if(FALSE){
  ### any code you want can be here ###
  print("Our 'if' statement is false.")
}

print("End run of code.")

x <- 7
x <- -4
x <- "word"
if(is.numeric(x)){
  print("our statement is true")
}else{
  print("our statement is false")
}

x <- 42

# before you run the following code, answer the questions in the comments
if(x < 50){ # WILL THIS BE TRUE OR FALSE?
  print("Yes, x is less than 50.")
}else if(x > 35){ # WILL THIS BE TRUE OR FALSE?
  print("Yes, x is greater than 35.")
}else{
  print("None of our criteria were met.")
}

if(x < 50){ 
  if(x > 35){ 
   print("Yes, x is greater than 35.")
  }
}else{
  print("None of our criteria were met.")
}

1:10

1:10 > 7
!(1:10 > 7)

if(1 < 7){
  print("One is less than seven.")
}

if(!(1 < 7)){
  print("The conditional is FALSE, so this statement should not print.")
}


?any

if(any(iris[, 1] > 7)){
  print("Found sepal length greater than 7.")
} else {
  print("Did NOT find sepal length greater than 7.")
}


if(sum(iris[, 1] > 7) >= 1){
  print("Found sepal length greater than 7.")
} else {
  print("Did NOT find sepal length greater than 7.")
}

sum(iris[, 1] > 7) >= 1
sum(iris[, 1] > 7)

Day 1 or 2 practice problems

#set working directory
#import dataset
data(mtcars)

#understand structure of mtcars
str(mtcars)
head(mtcars)
colnames(mtcars)

#calculate the average mpg of the cars
mean(mtcars$mpg)
sum(mtcars$mpg)/length(mtcars$mpg)

#name the calculation of the mean of mpg as "avg.mpg"
avg.mpg <- mean(mtcars$mpg)
avg.mpg

#what type of class is avg_mpg?
class(avg.mpg) #numeric

#calculate the standard deviation of mpg
sqrt(sum((mtcars$mpg - avg.mpg)^2)/(length(mtcars$mpg)-1))

sqrt(sum((mtcars$mpg  - avg.mpg)^2)/(length(mtcars$mpg) - 1))
sd(mtcars$mpg)
```R