Creating a function
In R, most of people used build-in functions which are very useful
for all basics statistical analyses. When you need to do more complex
statistics you can use packages.
There is almost every current statistical functions and more in the
CRAN repository. However, time to time you will need to build your own function. Usually, when I build a function is because for
loop will not be efficient and that I need to put different parameters that I can change.
I will take here an easy example that I have previously done for my work.
I wanted to build a short function to draw maps and save them if I needed to.
The data
There is two tables in this example. The first one data_plot_scale.txt is data of forest plots location, with different information such as the trees status, the (living or dead), mark (NA for no marks, DBH for trees with diameter information), dead for dead trees position only and mortality for mortality pattern), the stands names (Stand_ID.m), lng for longitude and lat for latitude coordinates and a result like state 1 to 3 in the column pattern_state. The second table is very similar but at the stand scale data_stand_scale.txt.
Those data represent results of spatial patterns (aggregation, repulsion or random) in forest at the plot and stand scales. Because several plots, which could have different patterns states, are in one stands, the stands patterns could be different from the plots scale. Also, because I needed a tree threshold to compute spatial patterns, there is few plots and stands that cannot be analyse for the dead status (stands #4, for example).
data.table 1.10.0 The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way Documentation: ?data.table, example(data.table) and browseVignettes("data.table") Release notes, videos and slides: http://r-datatable.com Status mark Stand_ID.m lng lat pattern_state 1: dead dead 17 20.11335 49.18592 state2 2: dead dead 17 20.11335 49.18592 state2 3: dead dead 17 20.11335 49.18592 state2 4: dead dead 17 20.11794 49.18627 state2 5: dead dead 17 20.11798 49.18874 state2 --- 1544: living NA 33 NA NA state2 1545: living NA 33 NA NA state2 1546: living NA 33 NA NA state2 1547: living NA 33 NA NA state1 1548: living NA 33 NA NA state1
When doing functions you will need first to create arguments (vector) that will be used in the function. In this example, I will use status as a proxy for Status (note the S uppercase difference), markk for selecting mark, and then just 2 others FALSE or TRUE vector is I want to have the graphics outputs with the stands and if I want to save same or not in the current directory.
The function
Here you can use the freshly build function map_draw
that we just
created. The status can be "living"
or "dead"
.
For the markk
note that you need to put NA
without any quote. Also,
you can put just F for FALSE or write FALSE, and the same for TRUE,
the result will be the same.
I edit the graph using Inkscape to put a red arrow on the plots/stands that appear only for living status.