615 R LABS


HW: homework for that week

IC: in class exercises

1| HW



1.1| File organization



1.2| Cottonwood

cottonwood <- read.csv("Data/cottonwood.csv")



1.3| Tree_height

Tree_height <- 15

Tree_circ <- pi*Tree_height

Tree_circ
## [1] 47.12389

little doodle, the end.
little doodle, the end.


1| IC

See in class handout for specifics. I did not create a workflow for this day.

little doodle, the end.
little doodle, the end.


2| HW

[coming soon]

little doodle while you wait.
little doodle while you wait.


2| IC

Importing Data | 9.2.26

Exercise 1

plants <- c("Orange", "Plum", "Lemon", "Peach", "Apple")

height <- as.numeric(c(5, 10, 4, 3, 2))

presence <- c(T, F, T, T, F)

width <- as.numeric(c(0.2, 0.5, 0.6, 0.2, 0.1))

df <- data.frame(plants, height, width, presence)

write.csv(df, "homemade_df_class4.csv")

cats <- read.csv("homemade_df_class4.csv", row.names = 1)

half <- cats$height*0.5

cats <- cbind(cats, half)



Exercise 2

new <- c(32, 14, "100")
str(new)
##  chr [1:3] "32" "14" "100"
coerced <- as.numeric(new)
str(coerced)
##  num [1:3] 32 14 100
dec <- c(5.2, 3.4, 4.4, 5.5, 3.4)
str(dec)
##  num [1:5] 5.2 3.4 4.4 5.5 3.4
coerced_2 <- as.integer(dec)
str(coerced_2)
##  int [1:5] 5 3 4 5 3
num <- c(3, 9, 11, 15) 

coerced_3 <- as.character(num)
str(coerced_3)
##  chr [1:4] "3" "9" "11" "15"
#if you want to create categories with your data..



Exercise 3

logical <- ifelse(cats$half >= 2, "Pass", "Fail")

cats <- cbind(cats, logical)

cats$logical
## [1] "Pass" "Pass" "Pass" "Fail" "Fail"
new <- factor(cats$logical, levels=c("Pass", "Fail"))
str(new)
##  Factor w/ 2 levels "Pass","Fail": 1 1 1 2 2
little doodle, the end.
little doodle, the end.


3| HW

[coming soon]

little seagull while you wait.
little seagull while you wait.


3| IC

[coming soon]

little doodle while you wait.
little doodle while you wait.


Videos

1

File Management: Video 1

Code

[no code for this video.]

little doodle, the end.
little doodle, the end.


2

Objects: Video 2

Packages: Video 3

Importing Data & Data Types: Video 4

Code

# Creating objects
# we assign values to objects

# object_name <- value

weight_kg <- 55
weight_kg
## [1] 55
# math with objects

weight_lb <- 2.2 * weight_kg
weight_lb
## [1] 121
# Functions

#getwd()

#print()

# Getting help

#?print

name <- "Bronco"

print(name, quote = TRUE)
## [1] "Bronco"
print(name, quote = FALSE)
## [1] Bronco
print(name, quote = TRUE)
## [1] "Bronco"
# Data Structures
# student heights: 1.7, 1.8, 1.6, 1.75
# vectors are groups of values
# create a vector with the c() function

# Vector with numbers
length_cm <- c(10, 15, 50)
length_cm
## [1] 10 15 50
# Character vector
animals <- c("mouse", "rat", "dog")
animals
## [1] "mouse" "rat"   "dog"
# quotes are important!
#THE FOLLOWING DOESNT WORK
#animals <- c(mouse, rat, dog)

# Vector from objects
weights <- c(weight_kg, weight_lb)
weights
## [1]  55 121
weight_bad <- c(40, 88)

# Data frames
# data frames have rows and columns

animal_length <- data.frame(animals, length_cm)
head(animal_length)
##   animals length_cm
## 1   mouse        10
## 2     rat        15
## 3     dog        50
# install.packages("stringr")

# Load the stringr package
library(stringr)

#?str_replace_all

# Replace spaces with underscores
sentence <- "I can write code"
sentence_under <- str_replace_all(string = sentence,
                                 pattern = " ",
                                 replacement = "_")
sentence_under
## [1] "I_can_write_code"
# tidyverse
# install.packages("tidyverse")

# Load the tidyverse package
library(tidyverse)

#?tidyverse

citation("tidyverse")
## To cite package 'tidyverse' in publications use:
## 
##   Wickham H, Averick M, Bryan J, Chang W, McGowan LD, François R,
##   Grolemund G, Hayes A, Henry L, Hester J, Kuhn M, Pedersen TL, Miller
##   E, Bache SM, Müller K, Ooms J, Robinson D, Seidel DP, Spinu V,
##   Takahashi K, Vaughan D, Wilke C, Woo K, Yutani H (2019). "Welcome to
##   the tidyverse." _Journal of Open Source Software_, *4*(43), 1686.
##   doi:10.21105/joss.01686 <https://doi.org/10.21105/joss.01686>.
## 
## A BibTeX entry for LaTeX users is
## 
##   @Article{,
##     title = {Welcome to the {tidyverse}},
##     author = {Hadley Wickham and Mara Averick and Jennifer Bryan and Winston Chang and Lucy D'Agostino McGowan and Romain François and Garrett Grolemund and Alex Hayes and Lionel Henry and Jim Hester and Max Kuhn and Thomas Lin Pedersen and Evan Miller and Stephan Milton Bache and Kirill Müller and Jeroen Ooms and David Robinson and Dana Paige Seidel and Vitalie Spinu and Kohske Takahashi and Davis Vaughan and Claus Wilke and Kara Woo and Hiroaki Yutani},
##     year = {2019},
##     journal = {Journal of Open Source Software},
##     volume = {4},
##     number = {43},
##     pages = {1686},
##     doi = {10.21105/joss.01686},
##   }
# Importing data with read.csv
pikas <- read.csv("Data/data_pika.csv")

# check View window
View(pikas)

# see first few lines of data frame
head(pikas)
##        Date days Station UTM_Easting UTM_Northing                   Notes Vial
## 1  8/2/2018   10     LL3      449888      4435612 Heard and saw pika here  172
## 2 8/20/2018   12     WK7      449224      4434233                          191
## 3  8/2/2018   10     LL2      449888      4435516           Really fresh!  171
## 4 8/20/2018   12     WK8      449242      4434295                          192
## 5 8/20/2018   12    WK12      449076      4434440                          196
## 6 8/20/2018   12     LL1      449915      4435575                          202
##   Concentration_pg_g Plate Site Biweek Sex     elev
## 1           834.0471     3   LL      4   U 3278.479
## 2          1328.0385     4   WK      6   U 3616.446
## 3          1332.2918     3   LL      4   U 3305.235
## 4          1626.2902     4   WK      6   M 3606.109
## 5          1711.8800     4   WK      6   U 3580.776
## 6          1804.8702     5   LL      6   U 3295.538
# structure of data
str(pikas)
## 'data.frame':    109 obs. of  13 variables:
##  $ Date              : chr  "8/2/2018" "8/20/2018" "8/2/2018" "8/20/2018" ...
##  $ days              : int  10 12 10 12 12 12 11 9 9 12 ...
##  $ Station           : chr  "LL3" "WK7" "LL2" "WK8" ...
##  $ UTM_Easting       : int  449888 449224 449888 449242 449076 449915 449076 451462 451411 449012 ...
##  $ UTM_Northing      : int  4435612 4434233 4435516 4434295 4434440 4435575 4434440 4432989 4432986 4434384 ...
##  $ Notes             : chr  "Heard and saw pika here" "" "Really fresh!" "" ...
##  $ Vial              : int  172 191 171 192 196 202 184 168 167 195 ...
##  $ Concentration_pg_g: num  834 1328 1332 1626 1712 ...
##  $ Plate             : int  3 4 3 4 4 5 4 3 3 4 ...
##  $ Site              : chr  "LL" "WK" "LL" "WK" ...
##  $ Biweek            : int  4 6 4 6 6 6 5 4 4 6 ...
##  $ Sex               : chr  "U" "U" "U" "M" ...
##  $ elev              : num  3278 3616 3305 3606 3581 ...
# access a single column of data
head(pikas$Date)
## [1] "8/2/2018"  "8/20/2018" "8/2/2018"  "8/20/2018" "8/20/2018" "8/20/2018"
# convert character data to date data
pikas$Date <- as.Date(pikas$Date, format = "%m/%d/%Y")

head(pikas)
##         Date days Station UTM_Easting UTM_Northing                   Notes Vial
## 1 2018-08-02   10     LL3      449888      4435612 Heard and saw pika here  172
## 2 2018-08-20   12     WK7      449224      4434233                          191
## 3 2018-08-02   10     LL2      449888      4435516           Really fresh!  171
## 4 2018-08-20   12     WK8      449242      4434295                          192
## 5 2018-08-20   12    WK12      449076      4434440                          196
## 6 2018-08-20   12     LL1      449915      4435575                          202
##   Concentration_pg_g Plate Site Biweek Sex     elev
## 1           834.0471     3   LL      4   U 3278.479
## 2          1328.0385     4   WK      6   U 3616.446
## 3          1332.2918     3   LL      4   U 3305.235
## 4          1626.2902     4   WK      6   M 3606.109
## 5          1711.8800     4   WK      6   U 3580.776
## 6          1804.8702     5   LL      6   U 3295.538
# summary statistics
summary(pikas)
##       Date                 days         Station           UTM_Easting    
##  Min.   :2018-06-08   Min.   : 1.00   Length:109         Min.   :448884  
##  1st Qu.:2018-07-11   1st Qu.: 6.00   Class :character   1st Qu.:449132  
##  Median :2018-07-31   Median : 8.00   Mode  :character   Median :449299  
##  Mean   :2018-07-29   Mean   : 8.56                      Mean   :449667  
##  3rd Qu.:2018-08-20   3rd Qu.:12.00                      3rd Qu.:449888  
##  Max.   :2018-09-04   Max.   :13.00                      Max.   :451617  
##   UTM_Northing        Notes                Vial       Concentration_pg_g
##  Min.   :4432963   Length:109         Min.   : 98.0   Min.   :  834     
##  1st Qu.:4434091   Class :character   1st Qu.:133.0   1st Qu.: 2668     
##  Median :4434204   Mode  :character   Median :164.0   Median : 4261     
##  Mean   :4434135                      Mean   :161.8   Mean   : 5176     
##  3rd Qu.:4434383                      3rd Qu.:192.0   3rd Qu.: 7169     
##  Max.   :4435616                      Max.   :219.0   Max.   :13531     
##      Plate           Site               Biweek          Sex           
##  Min.   :1.000   Length:109         Min.   :1.000   Length:109        
##  1st Qu.:2.000   Class :character   1st Qu.:3.000   Class :character  
##  Median :3.000   Mode  :character   Median :4.000   Mode  :character  
##  Mean   :3.055                      Mean   :4.202                     
##  3rd Qu.:4.000                      3rd Qu.:6.000                     
##  Max.   :5.000                      Max.   :7.000                     
##       elev     
##  Min.   :3278  
##  1st Qu.:3407  
##  Median :3584  
##  Mean   :3526  
##  3rd Qu.:3602  
##  Max.   :3616
little doodle, the end.
little doodle, the end.