Skip to content

Commit

Permalink
Merge pull request swcarpentry#822 from chi-raag/unequal_vector
Browse files Browse the repository at this point in the history
added section on vector recycling to 09-vectorization.Rmd
  • Loading branch information
matthieu-bruneaux committed Apr 14, 2023
2 parents 5f41374 + d423512 commit b4e2899
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions episodes/09-vectorization.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,27 @@ m * -1
> >
> {: .solution}
{: .challenge}

> ## Tip: Operations on vectors of unequal length
>
> Operations can also be performed on vectors of unequal length, through
> a process known as *recycling*. This process automatically repeats the smaller vector
> until it matches the length of the larger vector. R will provide a warning
> if the larger vector is not a multiple of the smaller vector.
>
> ```{r}
> x <- c(1, 2, 3)
> y <- c(1, 2, 3, 4, 5, 6, 7)
> x + y
> ```
>
> Vector `x` was recycled to match the length of vector `y`
>
> ```{r, eval=FALSE}
> x: 1 2 3 1 2 3 1
> + + + + + + +
> y: 1 2 3 4 5 6 7
> -----------------------
> 2 4 6 5 7 9 8
> ```
>

0 comments on commit b4e2899

Please sign in to comment.