Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
r_programming_gl [2014/11/21 20:45]
glaroc
r_programming_gl [2014/11/21 21:31] (current)
glaroc old revision restored (2014/11/21 15:54)
Line 1: Line 1:
 +====== Knitr ======
 +Knitr is a package that can be used to generate dynamic reports or web pages from R code. The code is evaluated at the moment the report is generated. ​
 +
 +Code can be easily written in RStudio use the Markdown language. View this page in Markdown language, and view the resulting web page. 
 +
 +
 ====== Data Table ====== ====== Data Table ======
 Data table is a very useful package in R which allows to facilitate and to improve the efficiency of certain operations in R. Data tables are just like data frames. You can even create them from data frames. ​ Data table is a very useful package in R which allows to facilitate and to improve the efficiency of certain operations in R. Data tables are just like data frames. You can even create them from data frames. ​
Line 39: Line 45:
 </​file>​ </​file>​
  
-With tapply()+**With tapply()**
 <file rsplus> <file rsplus>
 system.time(t2<​-tapply(mydf$b,​mydf$a,​mean)) system.time(t2<​-tapply(mydf$b,​mydf$a,​mean))
 </​file>​ </​file>​
  
-With reshape2+**With reshape2**
 <file rsplus> <file rsplus>
 library(reshape2) library(reshape2)
Line 51: Line 57:
 </​file>​ </​file>​
  
-With plyr+**With plyr**
 <file rsplus> <file rsplus>
 library(plyr) library(plyr)
Line 57: Line 63:
 </​file>​ </​file>​
  
-With sqldf. This package allows one to write Structured Query Language commands to perfom queries on a data frame. ​+**With sqldf**. This package allows one to write Structured Query Language commands to perfom queries on a data frame. ​
 <file rsplus> <file rsplus>
 library(sqldf) library(sqldf)
Line 63: Line 69:
 </​file>​ </​file>​
  
-With a basic FOR loop+**With a basic FOR loop**
 <file rsplus> <file rsplus>
 ti1<​-proc.time() ti1<​-proc.time()
 t6<​-data.frame(letter=unique(mydf$a),​mean=rep(0,​26)) t6<​-data.frame(letter=unique(mydf$a),​mean=rep(0,​26))
 for (i in t6$letter ){ for (i in t6$letter ){
-  t6[t6$letter==i,​2]=mean(mydf+  t6[t6$letter==i,​2]=mean(mydf[mydf$a==i,​2]) 
 +
 +eltime<​-proc.time()-ti1 
 +eltime 
 +</​file>​ 
 + 
 +**With a parallelized FOR loop** 
 +<file rsplus>​ 
 +library(foreach) 
 +library(doMC) 
 +registerDoMC(4) #Four-core processor 
 +ti1<​-proc.time() 
 +t7<​-data.frame(letter=unique(mydf$a),​mean=rep(0,​26)) 
 +t7[,2] <- foreach(i=t7$letter,​ .combine='​c'​) %dopar% { 
 + ​mean(mydf[mydf$a==i,​2]) 
 +
 +eltime<​-proc.time()-ti1 
 +eltime 
 +</​file>​ 
 + 
 +====== RgoogleMaps! ====== 
 +  
 +<file rsplus>​ 
 +library(RgoogleMaps) 
 +myhome=getGeoCode('​Olympic stadium, Montreal'​);​ 
 +mymap<​-GetMap(center=myhome,​ zoom=14) 
 +PlotOnStaticMap(mymap,​lat=myhome['​lat'​],​lon=myhome['​lon'​],​cex=5,​pch=10,​lwd=3,​col=c('​red'​));​ 
 +</​file>​ 
 + 
 +====== Taxize ====== 
 +<file rsplus>​ 
 +library(taxize) 
 +spp<​-tax_name(query=c("​american beaver"​),​get="​species"​) 
 +fam<​-tax_name(query=c("​american beaver"​),​get="​family"​) 
 +correctname <- tnrs(c("​fraxinus americanus"​)) 
 +cla<​-classification("​acer rubrum",​ db = '​itis'​) 
 +</​file>​ 
 + 
 +====== Spocc ====== 
 +<file rsplus>​ 
 +library(spocc) 
 +occ_data <- occ(query = 'Acer nigrum',​ from = '​gbif'​) 
 +mapggplot(occ_data) 
 +</​file>​ 
 + 
 +Combine spocc and RgoogleMaps 
 +<file rsplus>​ 
 +occ_data <- occ(query = 'Puma concolor',​ from = '​gbif'​) 
 +occ_data_df=occ2df(occ_data) 
 +occ_data_df<​-subset(occ_data_df,​!is.na(latitude) & latitude!=0) 
 +mymap<​-GetMap(center=c(mean(occ_data_df$latitude),​mean(occ_data_df$longitude)),​ zoom=2) 
 +PlotOnStaticMap(mymap,​lat=occ_data_df$latitude,​lon=occ_data_df$longitude,​cex=1,​pch=16,​lwd=3,​col=c('​red'​));​ 
 +</​file>​ 
 + 
 +====== geonames ====== 
 + 
 +<file rsplus>​ 
 +library(geonames) 
 +options(geonamesUsername="​glaroc"​) 
 +res<​-GNsearch(q="​Mont Saint-Hilaire"​) 
 +res[,​c('​toponymName','​fclName'​)] 
 +dc<​-GNcities(45.4,​ -73.55, 45.7, -73.6, lang = "​en",​ maxRows = 10) 
 +dc[,​c('​toponymName'​)] 
 +</​file>​ 
 + 
 +