Last week, I wrote the first draft of a report summarizing my first (finished) PhD analyses. The following are a few R functions I discovered or appreciated as I ran the analyses, generated output, and wrote the report.
llist() in the Hmisc package:
When bundling output objects from a series of analyses into one object, I put everything in one list. Often, I want the object name in the list to be the same as the object name outside the list. To avoid double-typing object names (e.g., list(brt=brt, train=train, test=test)), I use llist, which gives the list objects their own name, so list(brt, train, test) is sufficient. I give credit to the PleinR (R user group at U.Laval) email list and this discussion for helping me find this function.
xtable() in the xtable package:
When using Markdown to generate a summary HTML file containing my organized notes, code, and results, I use xtable() to print data.frames of results in a nicely formatted table. I can then simply copy and paste this table into a Word file and click on one of the styles (either pre-defined or custom) to change the HTML-styled table into a manuscript-styled table. I like this function because I don't need to create a table from scratch by copy and pasting individual numbers. Nor do I need to fuss around with spaces and tabs if I tried to copy and paste printed data frames. I give credit to Étienne Bellemare Racine for introducing me to Markdown and the xtable() function.
The extra bit to remember is that the chunk needs to include the option results="asis"
n.trees <- as.data.frame(do.call(cbind, lapply(outboot, function(x) {
do.call(rbind, lapply(x, function(y) {
y[["brt"]][["n.trees"]]
}))
})))
colnames(n.trees) <- c("Sp1", "Sp2", "Sp3")
print(xtable(n.trees), type = "html", include.rownames = T)
Sp1 | Sp2 | Sp3 | |
---|---|---|---|
1 | 1500 | 3600 | 1800 |
2 | 2100 | 3500 | 2250 |
3 | 1800 | 3100 | 1650 |
4 | 2400 | 4150 | 1650 |
5 | 2600 | 3100 | 2400 |
6 | 2050 | 3350 | 2000 |
7 | 1750 | 3700 | 1300 |
8 | 2050 | 4000 | 1500 |
9 | 2250 | 3450 | 1900 |
10 | 1600 | 3000 | 1550 |
colour.palette(), a custom function from me nugget:
It's a wrapper around colorRampPalette and allows more fine-tuned control of the resulting gradient in your colour ramp. I used it to create a ramp that had more colours in the low ranges and fewer at the higher. Most of my values were in the low ranges, so I needed a finer colour resolution in that range. I stumbled across this function while simply trying to confirm the name of the colorRampPalette function for a question on the R-help list.
unionSpatialPolygons() in the maptools package:
I'm currently trying to plot a map that shows the boreal and hemiboreal regions of Canada. The original polygon is too detailed for my needs, since I'm simply using it to facilitate interpretation of my results. I suspect that merging the four classes into two (boreal and boreal alpine into boreal, hemiboreal and hemiboreal alpine into hemiboreal) will aid visualization. It seemed that unionSpatialPolygons should do what I need, but after a few hours of trying, I suspect perhaps my shapefile is either too big or has some issues since I keep getting errors about "non-coded intersections" (kind of like this). Either way, I thought it was still worth mentioning the function. As for credit, I found it via RSeek.org by searching "aggregate shapefile polygons" and finding a thread which mentioned merging files. I have yet to try the approaches in this blog about simplifying polygon shapefiles in R. Alternatively, there's always ArcGIS...
That's it for this week.
Disclaimer: I am an R user; not an R expert. I welcome suggestions, advice, and corrections on posts I've made.
No comments:
Post a Comment