| Start | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Using built-in functionals Built-in functionals: lapply(), sapply(), and mapply()R comes with built-in functionals, of which perhaps the most well-known are the apply family of functions. Here is a description of some of the most common apply functions:
lapply()
sapply()
mapply()
|
||||||||||||||||||
| Bulk File Loading for a large number of files which may need to be operated on in a similar process and with well structured file names. firstly a vector of the file names to be accessed must be created, there are multiple options for this: where
This is not necessarily faster than a for loop from testing but allows all files to be an element of a list without assigning them explicitly. Finally, we often need to load multiple packages at once.
This trick can do it quite easily by applying |
||||||||||||||||||
Combining multiple data.frames (lapply, mapply) In this exercise, we will generate four bootstrap linear regression models and combine the summaries of these models into a single data frame. At this point, we can take two approaches to inserting the names into the data.frame. If you're a fan of |
||||||||||||||||||
Using user-defined functionals User-defined functionalsUsers can create their own functionals to varying degrees of complexity. The following examples are from Functionals (opens new window) by Hadley Wickham: In the first case, The second example is a re-implementation of |
||||||||||||||||||
| Use anonymous functions with apply
Let's use the Now, imagine that you want to know the mean of each of these variables. One way to solve this might be to use a
In the same way we can calculate more meaningful values: Caveat: R has some built-in functions which are better for calculating column and row sums and means: Now, let's do a different and more meaningful task: let's calculate the mean only for those values which are bigger than (Note the difference in the mean of But, what if we don't want to use this function in the rest of our code? Then, we can use an anonymous function, and write our code like this: So, as we have seen, we can use Caveat: Since RemarksA function in the
However The relationship between Members of the |
| function | Input | Output |
|---|---|---|
apply | matrix, data.frame, or array | vector or matrix (depending on the length of each element returned) |
sapply | vector or list | vector or matrix (depending on the length of each element returned) |
lapply | vector or list | list |
vapply | vector or `list | vector or matrix (depending on the length of each element returned) of the user-designated class |
mapply | multiple vectors, lists or a combination | list |
See "Examples" to see how each of these functions is used.
Read Article
https://develop.horje.com/learn/1434/reference