Skip to content

Commit

Permalink
for() loop session by Austen
Browse files Browse the repository at this point in the history
  • Loading branch information
an-bui committed Feb 23, 2021
1 parent 739d30e commit 0c7ddb0
Show file tree
Hide file tree
Showing 7 changed files with 4,529 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.Rhistory
.RData
.Ruserdata
*.html
24 changes: 20 additions & 4 deletions 2021-02-08-function_writing/template-FUNctions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ $F = C*1.8 + 32$
We can write a function called `f_to_c` that converts fahrenheit to celsius.

```{r}
# create a function called `f_to_c`
# takes the argument `x`
f_to_c <- function(x) {
# calculating (x - 32)/1.8
# assign temporary variable called convert
convert <- (x - 32)/1.8
# return the value of convert
return(convert)
}
Expand All @@ -78,15 +83,24 @@ $C*1.8 + 32 = F$

```{r}
# creating a function called c_to_f
# using argument temp
c_to_f <- function(temp) {
# calculating conversion as temp * 1.8 + 32
conversion <- temp*1.8 + 32
# returning value of conversion
return(conversion)
}
```

And check to see if it works!
```{r}
c_to_f(26)
```

Expand Down Expand Up @@ -129,7 +143,7 @@ temp.conv("celsius", "fahrenheit", 25)

Try it out below!
```{r}
temp.conv("fahrenheit", "celsius", 80)
```

If you want to incorporate different types of arguments into your function, you can use the or operator: `||`
Expand Down Expand Up @@ -157,7 +171,9 @@ temp.conv <- function(from, to, x) {

Try using the function with the new arguments!
```{r}
temp.conv(from = "f", to = "c", 50)
temp.conv("c", 45)
```


Expand Down
163 changes: 163 additions & 0 deletions 2021-02-22-for_loops/for_loops_activity.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
title: "DANC - February 22, 2021"
output: html_notebook
editor_options:
chunk_output_type: inline
---

#### A. Why use the `for()` loop?
Imagine trying to do an iterative process over a large dataset -
```{r}
install.packages("vegan", dependences = TRUE) # install vegan package
library(vegan) # call vegan package
data(dune) # load dataset within the vegan package
help("dune") # learn more about this data set
# The dune meadow vegetation data, dune, has cover class values of 30 species (columns) on 20 sites (rows).
# Perhaps I want to know the abundance of each plant species across these sites:
sum(dune[, 1]) # the sum function "sums" our provided argument, which calls the first column of the dune dataset by indexing (using the brackets)
## what if we wanted to sum the first row instead? how would that code change?
sum(dune[, 2])
sum(dune[, 3])
sum(dune[, 4])
sum(dune[, 5])
# and so forth - is this the most efficient way to do something?
```

#### B. Let's write a `for()` loop

* The basic structure of a for loop is:

for (variable in a collection of variables) {
expression(s)
}

Dr. Allison Horst's: awesome diagram that explains for loops
https://twitter.com/allison_horst/status/1290774616038809600/photo/1

For loops in three easy steps:

1) What's your desired objective? For example, "I want to repeat the sum of all the columns in my data frame." This is your "for loop".
2) Where's your data? Usually this data is a vector (a string of things that are of the same kind, for example, a sequence of numbers: 1, 2, 3, 4)
3) Where are you going to put your result? You'll need to make a "holding vector" or a place to put your answer. Usually this is an empty vector that is filled with NA or NaN placeholders. Any data generated within your loop will be not be saved, unless you use **variable assignment** within the loop.

* For example, if we wanted to add 5 to each element of the vector x, and store it in vector y, we could do it with a `for()` loop:
```{r}
x <- 1:10 # make a vector to iterate over
x
y <- rep(NaN, length(x)) # create an object y to hold the answer by repeating the number 0 into a vector of the same length as x (i.e., 9 slots)
y
for (i in 1:length(x)) { # for every number in the vector x, starting at the first variable in position 1 to the last variable in position 10
y[i] <- x[i] + 5 # add five to it and store it in the vector y, our new values will overwrite y
}
y
# remember "i" refers to positions in a vector, NOT the number
```

* However, in R, we can do this much more efficiently using the <- command (because x is vectorized), so you don't need a `for()` loop for everything!
```{r}
y <- x + 5
y
```

*'i' can be whatever you like, it just has to be consistent across the loop!
```{r}
x <- 1:10
x
y <- rep(NaN, length(x))
y
for (puppies in 1:length(x)) {
y[puppies] <- x[puppies] + 1
}
y
```

* Example using a logical statement
```{r}
x <- c(1,3,5,7,9,11)
x
emptydat <- rep(NaN, length(x))
for (i in 1:length(x)){
emptydat[i] <- ifelse(x[i] > 5, "yes", "no")
}
emptydat
```

* Back to our dune example, how would we write loop to find each plant species abundance across the 20 sampled sites?
```{r}
```

#### Exercise 1

* Write a `for()` loop that (1) iterates over the numbers 1 to 10, (2) calculates the cube of each number and (3) saves the output in a storage vector.
```{r}
```

#### Exercise 2

* Write a `for()` loop that (1) iterates over the numbers 1 to 10, (2) adds each variable to the variable that succeeds it in vector x (e.g., 1 + 2; 2 + 3; etc.) and (3) saves the output in a storage vector.
```{r}
```

#### Exercise 3

* Write a `for()` loop that (1) iterates over the all the rows in the dune dataset in sequential order, (2) calculates the mean number of plant species within a site (remember, sites refer to the rows in the matrix) and (2) saves the output in a storage vector. HINT: you'll need make each row numeric BEFORE you calculate its mean with the as.numeric() function.
```{r}
```

* You can also write nested for loops:
```{r}
x <- 1:10
x
y <- rep(NaN, length(x))
y
for (i in 1:length(x)) {
temp.number <- x[i] + 1 # what happens to "temp.number" each time a for loop repeats and iteration?
for (j in 1:length(x)) {
y[i] <- temp.number - 1
}
}
y
```

### Exercise 4
Write a nested for loop of your choice!
```{r}
```






2,046 changes: 2,046 additions & 0 deletions 2021-02-22-for_loops/for_loops_activity.nb.html

Large diffs are not rendered by default.

Loading

0 comments on commit 0c7ddb0

Please sign in to comment.