From 4b333f6308e6a91999de2febe435200fbdf3ed49 Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 18 Jul 2023 11:30:19 -0700 Subject: [PATCH] vector-functions concept --- concepts/vector-functions/.meta/config.json | 5 ++ concepts/vector-functions/about.md | 87 +++++++++++++++++++++ concepts/vector-functions/introduction.md | 67 ++++++++++++++++ concepts/vector-functions/links.json | 14 ++++ config.json | 5 ++ 5 files changed, 178 insertions(+) create mode 100644 concepts/vector-functions/.meta/config.json create mode 100644 concepts/vector-functions/about.md create mode 100644 concepts/vector-functions/introduction.md create mode 100644 concepts/vector-functions/links.json diff --git a/concepts/vector-functions/.meta/config.json b/concepts/vector-functions/.meta/config.json new file mode 100644 index 00000000..ede7d642 --- /dev/null +++ b/concepts/vector-functions/.meta/config.json @@ -0,0 +1,5 @@ +{ + "authors": ["colinleach"], + "contributors": [], + "blurb": "Many R functions can operate on entire vectors without the need for explicit loops." +} \ No newline at end of file diff --git a/concepts/vector-functions/about.md b/concepts/vector-functions/about.md new file mode 100644 index 00000000..020d1afc --- /dev/null +++ b/concepts/vector-functions/about.md @@ -0,0 +1,87 @@ +# About + +This is a big concept in R. +To help make sense of the possibilities, it can be useful to consider the dimensionality of the input(s) and output of the various functions. + +## Vector-in, scalar-out + +We already saw functions that take in a vector and return a single, scalar-like value: + +```R +> v <- 1:5 +> sum(v) +[1] 15 +> length(v) +[1] 5 +``` + +Many statistical functions are also in this category, such as `mean()`. + +For logical vectors, there are `all()` and `any()`, which return a single `TRUE` or `FALSE`. + +## Vector-in, vector-out + +Many functions in R will operate on entire vectors, often giving vector output. +This includes most of the mathematical functions: + +```R +> sq <- c(4, 9, 16, 25) +> sqrt(sq) # square root +[1] 2 3 4 5 +``` + +This is not just concise and convenient, avoiding the need for loops, list comprehensions or recursion. +In R, vector functions often run much faster than these more familiar techniques. + +You already used vectorized functions more than you probably realized. Compare these: + +```R +> v <- c(2, 7, 9) +> w <- c(3, 1, 5) +> v + w +[1] 5 8 14 +> "+"(v, w) +[1] 5 8 14 +``` + +The familiar infix operators are just syntactic sugar for the underlying vectorized function! In this case `"+"()`. + +For sorting a vector, there is `sort()` to return the values and `order()` to return the indices: + +```R +> v <- c("I", "am", "not", "in", "order") +> sort(v) +[1] "am" "I" "in" "not" "order" +> order(v) +[1] 2 1 4 3 5 +``` + +There are also functions to produce cumulative vector outputs, operating on the input vector left-to-right: + +```R +> cumsum(1:5) +[1] 1 3 6 10 15 # sums +> cumprod(1:5) +[1] 1 2 6 24 120 # factorials, for a range like this +``` + +## Multiple-vectors-in, vector-out + +An extension of this concept can also be used to compare vectors. +For example, consider the pairwise-max function `pmax()`: + +```R +> v +[1] 2 7 9 +> w +[1] 3 1 5 +> pmax(v, w) +[1] 3 7 9 # max of each pairwise comparison +``` + +This function and others like it also accept an arbitrary number of input vectors, not just two. + +## Vector-in, matrix-out + +Less common, but it is fairly easy to write functions that are apparently scalar-in, vector out. +When applied to vector input, the output is a 2-D `matrix` (covered in a later concept). diff --git a/concepts/vector-functions/introduction.md b/concepts/vector-functions/introduction.md new file mode 100644 index 00000000..c048a4ec --- /dev/null +++ b/concepts/vector-functions/introduction.md @@ -0,0 +1,67 @@ +# Introduction + +This is a big concept in R. +To help make sense of the possibilities, it can be useful to consider the dimensionality of the input and output of the various functions. + +## Vector-in, scalar-out + +We already saw functions that take in a vector and return a single, scalar-like value: + +```R +> v <- 1:5 +> sum(v) +[1] 15 +> length(v) +[1] 5 +``` + +Many statistical functions are also in this category, such as `mean()`. + +For logical vectors, there are `all()` and `any()`. + +## Vector-in, vector-out + +Many functions in R will operate on entire vectors, often giving vector output. +This includes most of the mathematical functions: + +```R +> sq <- c(4, 9, 16, 25) +> sqrt(sq) # square root +[1] 2 3 4 5 +``` + +This is not just concise and convenient, avoiding the need for loops, list comprehensions or recursion. +In R, vector functions often run much faster than these more familiar techniques. + +For sorting a vector, there is `sort()` to return the values and `order()` to return the indices: + +```R +> v <- c("I", "am", "not", "in", "order") +> sort(v) +[1] "am" "I" "in" "not" "order" +> order(v) +[1] 2 1 4 3 5 +``` + +There are also functions to produce cumulative vector outputs, operating on the input vector left-to-right: + +```R +> cumsum(1:5) +[1] 1 3 6 10 15 # sums +> cumprod(1:5) +[1] 1 2 6 24 120 # factorials, for a range like this +``` + +## Multiple-vectors-in, vector-out + +An extension of this concept can also be used to compare vectors. +For example, consider the pairwise-max function `pmax()`: + +```R +> v +[1] 2 7 9 +> w +[1] 3 1 5 +> pmax(v, w) +[1] 3 7 9 # max of each pairwise comparison +``` diff --git a/concepts/vector-functions/links.json b/concepts/vector-functions/links.json new file mode 100644 index 00000000..f0856daf --- /dev/null +++ b/concepts/vector-functions/links.json @@ -0,0 +1,14 @@ +[ + { + "url": "https://intro2r.com/using-functions-in-r.html", + "description": "Functions in R" + }, + { + "url": "https://intro2r.com/vectors.html#vec_ord", + "description": "Ordering vectors" + }, + { + "url": "https://intro2r.com/vectors.html#vectorisation", + "description": "Vectorisation" + } +] \ No newline at end of file diff --git a/config.json b/config.json index b51aafcc..b43a9034 100644 --- a/config.json +++ b/config.json @@ -586,6 +586,11 @@ "uuid": "2751b6f2-7d71-4397-b063-9bf927a57756", "slug": "booleans", "name": "Booleans" + }, + { + "uuid": "68d54762-d212-4e4c-b2b7-815acd13de2c", + "slug": "vector-functions", + "name": "Vector Functions" } ], "key_features": [