Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
r_programming_gl [2014/11/21 20:41] 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 8: | Line 14: | ||
| library(data.table) | library(data.table) | ||
| </code> | </code> | ||
| - | |||
| - | <code rsplus> | ||
| - | library(reshape2) | ||
| - | library(data.table) | ||
| - | library(plyr) | ||
| - | mydf=data.frame(a=rep(LETTERS,each=1e6),b=rnorm(26*1e6)) | ||
| - | mydt=data.table(mydf) | ||
| - | setkey(mydt,a) | ||
| - | mydt['F'] | ||
| - | # Returns all rows with column a (the key) equal to F | ||
| - | mydt[,mean(b),by=a] | ||
| - | # Gives the mean value of column b for each letter in column a. | ||
| - | # Compare | ||
| - | system.time(t1<-mydt[,mean(b),by=a]) | ||
| - | # 0.314 secs | ||
| - | # With tapply() | ||
| - | system.time(t2<-tapply(mydf$b,mydf$a,mean)) | ||
| - | # 7.239 secs | ||
| - | # With reshape2 | ||
| - | meltdf=melt(mydf) | ||
| - | system.time(t3<-dcast(meltdf,a~variable,mean)) | ||
| - | # 4.453 secs | ||
| - | # With reshape plyr | ||
| - | system.time(t4<-ddply(mydf,.(a),summarize,mean(b))) | ||
| - | # 2.288 secs | ||
| - | # With a FOR loop | ||
| - | ti1<-proc.time() | ||
| - | t5<-data.frame(letter=unique(mydf$a),mean=rep(0,26)) | ||
| - | for (i in t5$letter ){ | ||
| - | t5[t5$letter==i,2]=mean(mydf[mydf$b==i,2]) | ||
| - | } | ||
| - | eltime<-proc.time()-ti1 | ||
| - | </code> | ||
| - | |||
| - | |||
| - | ====== Data Table ====== | ||
| - | |||
| - | library(data.table) | ||
| Generate very long data frame with one column with letters, and one column with random numbers | Generate very long data frame with one column with letters, and one column with random numbers | ||
| - | <file | rsplus> | + | <file rsplus> |
| mydf=data.frame(a=rep(LETTERS,each=1e5),b=rnorm(26*1e5)) | mydf=data.frame(a=rep(LETTERS,each=1e5),b=rnorm(26*1e5)) | ||
| </file> | </file> | ||
| Convert the data frame to a data table format. | Convert the data frame to a data table format. | ||
| - | <file | rsplus> | + | <file rsplus> |
| mydt=data.table(mydf) | mydt=data.table(mydf) | ||
| </file> | </file> | ||
| Each data table has to be assigned a key, which is one (or more) of the columns from the table. This key defines the basis for the organization and the sorting of the table. | Each data table has to be assigned a key, which is one (or more) of the columns from the table. This key defines the basis for the organization and the sorting of the table. | ||
| - | <file | rsplus> | + | <file rsplus> |
| setkey(mydt,a) | setkey(mydt,a) | ||
| </file> | </file> | ||
| Once the key is set, we can return all rows with column a (the key) equal to F | Once the key is set, we can return all rows with column a (the key) equal to F | ||
| - | <file | rsplus> | + | <file rsplus> |
| mydt['F'] | mydt['F'] | ||
| - | </file | + | </file> |
| Gives the mean value of column b for each letter in column a. | Gives the mean value of column b for each letter in column a. | ||
| - | <file | rsplus> | + | <file rsplus> |
| mydt[,mean(b),by=a] | mydt[,mean(b),by=a] | ||
| </file> | </file> | ||
| Let's compare the performance of Data table with other methods to achieve the same thing. | Let's compare the performance of Data table with other methods to achieve the same thing. | ||
| - | <file | rsplus> | + | <file rsplus> |
| system.time(t1<-mydt[,mean(b),by=a]) | system.time(t1<-mydt[,mean(b),by=a]) | ||
| </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) | ||
| meltdf=melt(mydf) | meltdf=melt(mydf) | ||
| Line 89: | Line 57: | ||
| </file> | </file> | ||
| - | With plyr | + | **With plyr** |
| - | <file | rsplus> | + | <file rsplus> |
| library(plyr) | library(plyr) | ||
| system.time(t4<-ddply(mydf,.(a),summarize,mean(b))) | system.time(t4<-ddply(mydf,.(a),summarize,mean(b))) | ||
| </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) | ||
| system.time(t5<-sqldf('SELECT a, avg(b) FROM mydf GROUP BY a')) | system.time(t5<-sqldf('SELECT a, avg(b) FROM mydf GROUP BY a')) | ||
| </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)) | ||
| Line 112: | Line 80: | ||
| </file> | </file> | ||
| - | ### With a parallelized FOR loop | + | **With a parallelized FOR loop** |
| - | <file | rsplus> | + | <file rsplus> |
| library(foreach) | library(foreach) | ||
| library(doMC) | library(doMC) | ||
| Line 128: | Line 96: | ||
| ====== RgoogleMaps! ====== | ====== RgoogleMaps! ====== | ||
| - | <file | rsplus> | + | <file rsplus> |
| library(RgoogleMaps) | library(RgoogleMaps) | ||
| - | myhome=getGeoCode('McGill Biology Department'); | + | myhome=getGeoCode('Olympic stadium, Montreal'); |
| mymap<-GetMap(center=myhome, zoom=14) | mymap<-GetMap(center=myhome, zoom=14) | ||
| PlotOnStaticMap(mymap,lat=myhome['lat'],lon=myhome['lon'],cex=5,pch=10,lwd=3,col=c('red')); | PlotOnStaticMap(mymap,lat=myhome['lat'],lon=myhome['lon'],cex=5,pch=10,lwd=3,col=c('red')); | ||
| Line 136: | Line 104: | ||
| ====== Taxize ====== | ====== Taxize ====== | ||
| - | <file | rsplus> | + | <file rsplus> |
| library(taxize) | library(taxize) | ||
| spp<-tax_name(query=c("american beaver"),get="species") | spp<-tax_name(query=c("american beaver"),get="species") | ||
| Line 145: | Line 113: | ||
| ====== Spocc ====== | ====== Spocc ====== | ||
| - | <file | rsplus> | + | <file rsplus> |
| library(spocc) | library(spocc) | ||
| occ_data <- occ(query = 'Acer nigrum', from = 'gbif') | occ_data <- occ(query = 'Acer nigrum', from = 'gbif') | ||
| Line 151: | Line 119: | ||
| </file> | </file> | ||
| - | ### Combine spocc and RgoogleMaps | + | Combine spocc and RgoogleMaps |
| - | <file | rsplus> | + | <file rsplus> |
| occ_data <- occ(query = 'Puma concolor', from = 'gbif') | occ_data <- occ(query = 'Puma concolor', from = 'gbif') | ||
| occ_data_df=occ2df(occ_data) | occ_data_df=occ2df(occ_data) | ||
| Line 162: | Line 130: | ||
| ====== geonames ====== | ====== geonames ====== | ||
| - | <file | rsplus> | + | <file rsplus> |
| library(geonames) | library(geonames) | ||
| options(geonamesUsername="glaroc") | options(geonamesUsername="glaroc") | ||
| Line 170: | Line 138: | ||
| dc[,c('toponymName')] | dc[,c('toponymName')] | ||
| </file> | </file> | ||
| + | |||
| + | |||
