This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set working directory | |
dir_path <- "path_to_text_files" | |
setwd(dir_path) | |
# create vector of filenames | |
filenames <- list.files(dir_path) | |
# read in files to a list | |
docList <- lapply(filenames, scan, what = "character", sep = "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data(mtcars) | |
library(ggplot2) | |
plot1 <- ggplot(mtcars, aes(x = hp, y = mpg)) + | |
geom_point() + | |
geom_smooth(method = "lm") + | |
facet_wrap(~ cyl) + | |
theme_bw() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x <- c( | |
'knitr', # A general-purpose package for dynamic report generation in R. | |
# 'sqldf', # For running SQL statements on R data frames, optimized for convenience. | |
'randomForest', # Classification and regression based on a forest of trees using random inputs. | |
'arm', # R functions for processing lm, glm, svy.glm, mer and polr outputs. | |
'ggplot2', # An implementation of the Grammar of Graphics. | |
'gridExtra', # misc. high-level Grid functions | |
'plyr', # Tools for splitting, applying and combining data. | |
'tree', # Classification and regression trees. | |
'gbm', # Generalized Boosted Regression Models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(plyr) | |
data(mpg) | |
# create a data frame with averages and standard deviations | |
hwy.means <- ddply(mpg, c("class", "year"), summarize, hwy.avg = mean(hwy), hwy.sd = sd(hwy)) | |
# barplot with values over bars | |
ggplot(hwy.means) + | |
geom_bar(aes(class, hwy.avg, fill = factor(year)), position = "dodge", colour = "black", size = 0.3) + |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example of how to concatenate lots of variables into a formula without typing them out | |
# example from the High School and Beyond data set | |
hsb_df <- read.table("http://www.ats.ucla.edu/stat/R/notes/hs0.csv", header = TRUE, sep = ",") | |
# create the formula using variables from columns 4:8 and 10:11 as predictors | |
hsb_form <- formula( paste(c("math ~ 1", colnames(hsb_df[, c(4:8, 10:11)])), collapse = " + ") ) | |
# fit the model | |
fit1 <- lm(hsb_form, data = hsb_df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# =============================================================================== | |
# Name : centroid_perm | |
# Original author : Steven Worthington (sworthington@iq.harvard.edu) | |
# Affiliation : IQSS, Harvard University | |
# Date (mm/dd/yyyy) : 06/14/2012 | |
# Version : v0.8 | |
# Aim : exact permutation test for group differences | |
# =============================================================================== | |
# Goal: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from PIL.ExifTags import TAGS, GPSTAGS | |
def get_exif_data(image): | |
"""Returns a dictionary from the exif data of an PIL Image item. Also converts the GPS Tags""" | |
exif_data = {} | |
info = image._getexif() | |
if info: | |
for tag, value in info.items(): | |
decoded = TAGS.get(tag, tag) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# list with character vectors | |
text <- list(a = "all day I play @sworth with R", | |
b = "all night I play @sworth with R") | |
# extract letters after "@" in a single character vector | |
sub("^.*@(\\w+).*", "\\1", text$a) | |
# extract letters after "@" in a list of character vectors | |
gsub("^.*@(\\w+).*", "\\1", text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sp) | |
library(maptools) | |
# get North Carolina shape data | |
NC <- readShapePoly(system.file("shapes/sids.shp", package = "maptools")[1], | |
IDvar = "FIPSNO", proj4string = CRS("+proj=longlat +ellps=clrk66")) | |
# plot polygons | |
plot(NC, border = "blue", axes = TRUE, las = 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(lme4) | |
head(sleepstudy) | |
summary(sleepstudy) | |
sapply(sleepstudy, class) | |
# Fit model using the original data: | |
d <- sleepstudy | |
d$Subject <- factor(rep(1:18, each=10)) | |
fm1 <- lmer(Reaction ~ Days + (Days|Subject), d) |
OlderNewer