Skip to content

Instantly share code, notes, and snippets.

n <- 50
m <- 50
set.seed(1)
mu <- -0.4
sig <- 0.12
x <- matrix(data=rlnorm(n*m, mu, sig), nrow=m)
library(fitdistrplus)
## Fit a log-normal distribution to the 50 random data set
f <- apply(x, 2, fitdist, "lnorm")
@mages
mages / gist:1544009
Created December 31, 2011 13:30
Search and replace string across files with R
## The following example demonstrates
## how a serach and replace string task
## can be peformed with R across several files
## Create two text files with content
filenames <- c( tempfile(), tempfile() )
for( f in filenames ){
cat("We wish you a Merry Christmas!\n\nBest regards\n", file=f)
}
@mages
mages / gist:1547765
Created January 1, 2012 16:55
Plotting mammal dataset
## Response to
## http://r-ecology.blogspot.com/2011/12/weecology-can-has-new-mammal-dataset.html
## Code from R-ecology
# URLs for datasets
comm <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_communities.csv"
refs <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_references.csv"
sites <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_sites.csv"
spp <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_species.csv"
trap <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_trapping.csv"
@mages
mages / mammaldataset.R
Created January 1, 2012 17:02 — forked from sckott/mammaldataset.R
Looking at the Weecology mammal dataset
# setwd("/Mac/R_stuff/Blog_etc/Mammal_Dataset")
# URLs for datasets
comm <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_communities.csv"
refs <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_references.csv"
sites <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_sites.csv"
spp <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_species.csv"
trap <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_trapping.csv"
# read them
@mages
mages / mammaldataset.R
Created January 1, 2012 17:02 — forked from sckott/mammaldataset.R
Looking at the Weecology mammal dataset
# setwd("/Mac/R_stuff/Blog_etc/Mammal_Dataset")
# URLs for datasets
comm <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_communities.csv"
refs <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_references.csv"
sites <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_sites.csv"
spp <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_species.csv"
trap <- "http://esapubs.org/archive/ecol/E092/201/data/MCDB_trapping.csv"
# read them
@mages
mages / R_vignette_survey.R
Created January 8, 2012 20:58
R vignette survey analysis
### Copyright 2012 Markus Gesmann
### markus dot gesmann at gmail dot com
###
### This file is made available under the terms of the GNU General Public
### License, version 2, or at your option, any later version,
### incorporated herein by reference.
###
### This program is distributed in the hope that it will be
### useful, but WITHOUT ANY WARRANTY; without even the implied
### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
@mages
mages / gist:1629150
Created January 17, 2012 21:50
Plot credit rating by country
library(XML)
library(googleVis)
url <- "http://en.wikipedia.org/wiki/List_of_countries_by_credit_rating"
page <- readLines(url)
gvisCountryRating <- function(rating.agent="S&P", which=3,
startRating=4, page=page){
x <- readHTMLTable(page, which=which)
levels(x$Rating) <- substring(levels(x$Rating), startRating,
@mages
mages / gist:1694442
Created January 28, 2012 14:15
Say it in R with by, apply and friends
## Markus Gesmann, January 2012
## Please install the following R packages first:
## data.table, doBy, plyr, reshape, sqldf, e.g. via
## install.packages(c("data.table", "doBy", "plyr", "reshape", "sqldf"))
f <- function(x) x^2
sapply(1:10, f)
do.call("rbind", as.list(
by(iris, list(Species=iris$Species), function(x){
@mages
mages / gist:1744195
Created February 5, 2012 08:50
Plot Apple's share price with googleVis and gvisAnnotatedTimeLine
## Markus Gesmann, February 2012
## This R script requires the googleVis package,
## a browser with Flash and an Internet connection.
gvisWeeklyStockData <- function(tckr = "AAPL",
chartid ="Apple",
start.year=1984){
require(googleVis)
d <- Sys.time()
@mages
mages / gist:1971411
Created March 4, 2012 08:45
Animated Geo Chart using World Bank Data
## Copyright Markus Gesmann, March 2011
## Distributed under GPL 2 or later
## Animated Geo Charts based on ideas and code by:
## Manoj Ananthapadmanabhan and Anand Ramalingam
## Here we animate the life expectancy by country from 1960
## This demo requires the 'WDI' and 'Hmisc' packages
library(WDI)
library(Hmisc)