This is a text. but changed this feel free to delete this.
- WHy
- Don't
- I Try
- A Bulleted list?
I added this.
| aPlotFunction <- function(hh, ss, sz){ | |
| zp1 <- qplot(data = cars, x = dist, y = speed, | |
| colour = I(hsv(hh/255, 1, 1)), | |
| shape = I(ss), | |
| size = I(sz)) | |
| print(zp1 + theme_bw()) | |
| } | |
| manipulate( | |
| aPlotFunction(hh, ss, sz), |
This is a text. but changed this feel free to delete this.
I added this.
| # improved list of objects | |
| # From http://stackoverflow.com/a/4827843/479554 | |
| .ls.objects <- function (pos = 1, pattern, order.by, | |
| decreasing=FALSE, head=FALSE, n=5) { | |
| napply <- function(names, fn) sapply(names, function(x) | |
| fn(get(x, pos = pos))) | |
| names <- ls(pos = pos, pattern = pattern) | |
| obj.class <- napply(names, function(x) as.character(class(x))[1]) | |
| obj.mode <- napply(names, mode) |
| N <- 1000 | |
| nDims <- 2 | |
| randomNumbers <- rnorm(nDims * N, 0, 1) | |
| randomNumbers <- matrix(randomNumbers, ncol = nDims) | |
| plot(randomNumbers) | |
| radius = sqrt(rowSums(randomNumbers ^ 2)) | |
| randomSphere <- 1/radius * randomNumbers | |
| plot(randomSphere) |
| doInstall <- TRUE | |
| toInstall <- c("ggplot2") | |
| if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")} | |
| lapply(toInstall, library, character.only = TRUE) | |
| # Make some random data: | |
| randPoints <- data.frame(x = runif(1000), y = runif(1000)) | |
| randPoints$color <- hsv(runif(1000), runif(1000), runif(1000)) | |
| zp1 <- ggplot(randPoints, |
| library(digest) | |
| set.seed(1) | |
| (x <- sample(1e9, size=6)) | |
| # [1] 265508664 372123900 572853364 908207790 201681932 898389685 | |
| ## To hash R's internal representation of these numbers | |
| strtoi(substr(sapply(x, digest), 28, 32), 16L) %% 1e3 | |
| # [1] 552 511 233 293 607 819 |
| #' Finds the local maxima (peaks) in the given vector after smoothing the data | |
| #' with a kernel density estimator. | |
| #' | |
| #' First, we smooth the data using kernel density estimation (KDE) with the | |
| #' \code{\link{density}} function. Then, we find all the local maxima such that | |
| #' the density is concave (downward). | |
| #' | |
| #' Effectively, we find the local maxima with a discrete analogue to a second | |
| #' derivative applied to the KDE. For details, see this StackOverflow post: | |
| #' \url{http://bit.ly/Zbl7LV}. |
| myFunction <- function(x){prod(1:x)} | |
| myFunction(10) | |
| system.time(lapply(1:10000, myFunction)) | |
| library(parallel) | |
| myCluster <- makeCluster(detectCores()) | |
| clusterExport(myCluster, ls()) | |
| system.time(parSapply(myCluster, 1:10000, myFunction)) | |
| stopCluster(myCluster) |
| doInstall <- TRUE | |
| toInstall <- c("ggplot2", "poLCA", "reshape2") | |
| if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")} | |
| lapply(toInstall, library, character.only = TRUE) | |
| ANES <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANES.csv") | |
| ANES <- ANES[ANES$year == 2008, -c(1, 11, 17)] # Limit to just 2008 respondents, | |
| head(ANES) # remove some non-helpful variables | |
| # Adjust so that 1 is the minimum value for each variable: | |
| ANES <- data.frame(apply(ANES, 2, function(cc){ cc - min(cc, na.rm = T) + 1 })) |
| doInstall <- TRUE # Change to FALSE if you don't want packages installed. | |
| toInstall <- c("maptools", "rgdal", "ggplot2", "spatstat", "RColorBrewer") | |
| if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")} | |
| lapply(toInstall, library, character.only = TRUE) | |
| # Taking an online compressed shapefile, and opening it in R | |
| # From http://stackoverflow.com/a/3053883 | |
| temp <- tempfile() # 110th & 111th Congressional District Shapefiles | |
| download.file("http://www.census.gov/geo/cob/bdy/cd/cd110shp/cd99_110_shp.zip", | |
| temp) # See http://www.census.gov/geo/www/cob/cd110.html#shp |