Map, Reduce, Filter and Lambda are four commonly-used techniques in functional programming. Like Python, the R programming has these features as well. This tutorial will cover the basic examples of these four elements in the R programming language.
Lambda
Lambda can be seen as a short (normally one line) function definition. There is no particular syntax for lambda in R, except that you don’t need to assign the function to a variable (function name), and also you will need to enclose the function using brackets or curly brace. For example:
(function add(x, y) x+y) {function add(x, y) x+y}
Both define a lambda function that is used to add two numbers. You can pass the lambda function as a parameter to other functions e.g. Map, Reduce or Filter.
Map
The function Map allows the mapping from one vector to another using a map function, which can be specified by lambda. For example, let’s define a vector from 1 to 100.
x=1:100
And, if we want to convert each element of the vector to its doubles, so we can pass a lambda to the Map function, like this:
y=Map({function (a) a*2}, x)
However, the variable y will store a list of vectors instead of a single vector e.g. y[1][1] = 2 and y[2][1] = 4, but you can use the unlist function to unroll the list of vectors into a single vector, so the following will return what is expected:
unlist(y) [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 [33] 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 [65] 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 186 188 190 192 [97] 194 196 198 200
Reduce
The function Reduce will perform the function on a list of vectors one by one, and finally return a single value. For example,
x=seq(1,10,0.5) [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
Then, we can add these numbers by:
Reduce(function (x, y) x+y, x) [1] 104.5
Please note that, you can omit the curly brace or brackets when passing lambda to these functions e.g. Map, Reduce, Filter (syntax sugar). The above example is equivalent to the following, but more concise:
y = 0 for (i in 1:length(x)) { y = y + x[i] }
Filter
The function Filter will remove all elements when they do not satisfy the condition (function returns false). For example, let’s make a vector that contains natural numbers from 1 to 10:
x=1:10
Now, let’s keep all the even numbers which should be 2, 4, 6, 8 and 10. So we can Filter the list by defining what is a even number.
Filter(function (x) x%%2==0, x) [1] 2 4 6 8 10
R Tutorial
- R Tutorial – Map, Filter, Reduce, Lambda
- R Tutorial – Monte Carlo
- R Tutorial – Permutation
- R Tutorial – Sigmoid
- R Tutorial – Connecting to STEEMSQL via RODBC
- R Tutorial – How rich is SteemIt Wechat Group?
- R Tutorial – Knowing when a Steem Whale vote?
- R Tutorial – How to Connect to SteemSQL via RStudio?
- R Tutorial – Using R to Fit Linear Model – Predit Weight over Height
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Make More Storage Space by Moving App/Images/Videos to SD card on Android Smart Phones?
Next Post: Derangement Permutation Implementation using R Programming