Skip to content

Instantly share code, notes, and snippets.

@daijiang
daijiang / Data_animate_gganimate.R
Last active June 27, 2019 18:41 — forked from djj4tree/Data_animate_gganimate.R
Animate tree data collection gganimate R code
# snippet of code to animate data in order of collection
# a little bit of data to show how it works
df <- structure(list(sp = structure(c(2L, 2L, 3L, 1L, 1L, 3L, 1L, 3L,
2L, 3L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 2L, 3L, 3L, 1L, 3L, 3L, 1L,
3L, 3L, 3L, 3L, 1L, 3L, 1L, 1L, 3L, 2L, 3L, 2L, 1L, 1L, 2L, 2L,
1L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 2L,
3L, 3L, 2L, 2L, 3L, 2L, 1L, 1L, 3L, 2L, 3L), .Label = c("D",
"O", "P"), class = "factor"), dbh = c(19.36, 20.74, 10.13, 10.47,
10.34, 14.22, 10.17, 22.91, 6.58, 7.93, 14.44, 6.67, 20.93, 7.16,
if(!require(devtools)) install.packages("devtools")
devtools::install_github('rstudio/blogdown')
library(blogdown)
options(servr.daemon = TRUE)
install_hugo() # install Hugo
new_site() # create folders
# install_theme("gcushen/hugo-academic")
# install_theme('jbub/ghostwriter')
@daijiang
daijiang / climate_data_today.R
Created May 19, 2016 15:30
To get historical climate data for a specific date.
library(weatherData)
library(plyr)
library(dplyr)
library(ggplot2)
library(tidyr)
years = seq(1948, 2015, 1)
days = "-03-08"
alldays = paste0(years, days)
dat_climate = list(length = length(alldays))
@daijiang
daijiang / get_citation_keys.R
Last active May 19, 2016 15:29
Get citation keys from tex file; then get a subset of cited items as a bib file.
library(stringr)
library(RefManageR)
## to get citation keys (with forms: author2016title) from tex file
get_cite_key = function(tex){
text = readLines(tex)
# \cite*{}
key_1 = str_replace_all(grep("^.*cite[a-z]{0,7}\\{([A-Za-z0-9,-]*)\\}.*$",
x = text, value = T),
pattern = "^.*cite[a-z]{0,7}\\{([A-Za-z0-9,-]*)\\}.*$",
@daijiang
daijiang / fibonacci
Last active August 29, 2015 14:20
fibonacci numbers
# python version
known = {0:0, 1:1}
def fibonacci(n):
if n in known:
return known[n]
res = fibonacci(n-1) + fibonacci(n-2)
known[n] = res
return res
@daijiang
daijiang / Distance from lat and long
Last active August 29, 2015 14:19
Distance calculation based on latitude and longitude
# Here are codes to calculate distance of points based on their lat and long coordinations.
# The Python version is faster than R version.
# Data are presented within R code. For 9 points, in my old laptop,
# R took 0.011 seconds while Python used 0.00595 seconds.
# For a dataset with 1,000 million points, Python version finished in 20 mins,
# but R needs ~62 hours!
## R version. ============================================================
library(dplyr)
library(sp)
@daijiang
daijiang / sp_name_check
Created December 30, 2014 04:42
Codes in this file can be used to download species informations from the [Herbarium of UWSP](http://wisplants.uwsp.edu/index.html) and check your species names.
Working with datasets with inconsistent species names is annoying. For example, I am working on resurvey Pine Barrens in central Wisconsin that had been sampled in 1958. However, some species names have been changed overtime. Thus, it is necessary to sync them before data analysis. There is an excellent R package `[taxize](http://ropensci.org/tutorials/taxize_tutorial.html)` for this. However, it is global based thus sometimes can be overwhelming. And I just want to sync my species names with names from the Wisconsin State Herbarium.
Until today, I checked species names manually, i.e. search website one by one. This will be fine if I only have one dataset to deal with. Last week, I was trying to build a functional trait dataset from different sources, e.g. carbon and nitrogen before 2013 in one spreadsheet and from this year in another; leaf area and SLA in another; plant height and flower height in another, etc. In addition, it is no clear that whether these spreadsheet have consistent species names! Plus,