Skip to content

Commit

Permalink
Merge pull request #975 from datacarpentry/vector-order
Browse files Browse the repository at this point in the history
Show constant math first in vector math
  • Loading branch information
ethanwhite authored Sep 5, 2023
2 parents 9aa9205 + 8b0492a commit 1a8548f
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions materials/vectors-R.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,16 @@ area <- c(3, 5, 1.9, 2.7)

#### Vector math

* We can divide the count vector by the area vector to get a vector of the density of individuals in that area

```r
density <- count / area
```

* This works because when we divide vectors, R divides the first value in the first vector by the first value in the second vector, then divides the second values in each vector, and so on
* Element-wise: operating on one element at a time

* To perform the same operation on each value in a vector you can use constants
* Perform the same mathematical operation on each value in a vector by treating it like we would a single value
* So if we wanted to double all of the values in the `area` vector

```r
area * 2
```

* This works because when do this multiplication, R multiplies the first value in the vector by 2, then multiplies the second values in the vector by 2, and so on
* Element-wise: operating on one element at a time

* Remember - this isn't saved unless we store it
* So `area` hasn't changed

Expand All @@ -122,6 +116,16 @@ doubled_area <- area * 2
doubled_area
```

* We can also do element-wise math with multiple vectors of the same length
* Let's divide the count vector by the area vector to get a vector of the density of individuals in that area

```r
density <- count / area
```

* When we divide the two vectors, R divides the first value in the first vector by the first value in the second vector, then divides the second values in each vector, and so on
* Element-wise: operating on one element at a time

#### Filtering

* Subsetting or "filtering" is done using `[]`
Expand Down

0 comments on commit 1a8548f

Please sign in to comment.