Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repro1 #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
4 changes: 4 additions & 0 deletions scripts/02-plot-data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Read in library
library(ggplot2)

## Read in data
58 changes: 58 additions & 0 deletions scripts/03-functions-if.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
### How to write a function in R ####

# # Form of a function
# function_name <- function(inputs) {
# output_value <- do_somthing(inputs)
# return(output_value)
# }


calc_shrub_vol <- function(length, width, height) {
area <- length * width
volume <- area * height
return(volume)
}

calc_shrub_vol(0.8, 2, 6)

calc_shrub_vol <- function(length, width, height = 1) {
area <- length * width
volume <- area * height
return(volume)
}

calc_shrub_vol(1.3, 6) ##can run with two values by defining height = 1
calc_shrub_vol(1.3, 6, 3) ##can override hight = 1 and still run with three values
calc_shrub_vol(1.3, 6, height = 3) ##can also call the names of the variables within the function

#### How to use if statements in R ####

# Form of an if statement
# if (the conditional statement is TRUE ) {
# do something
# }

##if statement example 1
x <- 6
if(x > 5) {
print(x)
}

##if statment example 2
if(!file.exists("data/file1.csv")){
download.url("http://data.com")
}

##if, else if, and else statement
if(x > 5) {
print(x)
} else if(x < 5 & x > 1) {
print("somewhat small")
} else {
print("way too small")
}

##ifelse statement
ifelse(x==5, "yes", "no")

case_when()