View mutate_alternate.r
library(tidyverse)
datatab <- as.tibble(c(1:10))
# modulo division
datatab$value %% 2
# since we have alternating even and odd value in "value" variable
datatab %>%
mutate(valueplus = ifelse((value %% 2) == 0, "even", "odd"))
View col_name.r
# Problem:
# - row 2 of data file has non-data title that repeats every two columns
# - column 1 / row 1 header label is fine
# - the header in every even-numbered column applies to the next odd-humbered column (eg 2 applies to 3, 4 to 5, etc)
# - the header in those odd-numbered columns (3, 5, 7, etc) is read initially as an NA
# Solution
# - read column names only
# - hard code even and odd suffix
# - copy header value in those even columns to odd columns
View datefixLahman.R
#
library(Lahman)
data(Master)
#
# `debut` variable; create new version `debutDate`
Master$debutDate <- (as.Date(Master$debut, "%m/%d/%Y"))
Master$debutDate[is.na(Master$debutDate)] <-
as.Date(Master$debut[is.na(Master$debutDate)])
#
# `finalGame` variable; create new version `finalGameDate`
View gist:efdf9c772054131ca22f
title author date output
Testing Lahman 3.0
Martin Monkman
Sunday, August 31, 2014
html_document

This markdown document incorporates a variety of short scripts that draw on the various tables in the Lahman package. (See the Lahman project page on RForge for more details http://lahman.r-forge.r-project.org/.)

Note that some of scripts appear in the documentation of other R packages; in those cases, the original source is noted prior to the script.

View gist:0f92cba504f2e7f11bba
if (!require(wesanderson)) install.packages("wesanderson")
library(wesanderson)
# for more on the Wes Anderson colour palette:
# https://github.com/karthik/wesanderson#wes-anderson-palettes
# http://blog.revolutionanalytics.com/2014/03/give-your-r-charts-that-wes-anderson-style.html
#
#
#
# add some Wes Anderson "Grand Budapest Hotel" colour to print object "p2"
p2 + scale_fill_manual(values = wes.palette(4, "GrandBudapest"))
View gist:3c0da6afd58eb61e2c51
#
# setwd("D:/R_the software/datatrials/Lahman")
#
require(Lahman)
require(dplyr)
#
# throwing by position
# version 1 - "merge"
MasterFielding <- data.frame(merge(Master, Fielding, by="playerID"))
MasterFielding <- merge(Master, Fielding, by="playerID")
View gist:9190970
View gist:8798762
# CALCULATING PERCENTILES IN R
#
# a basic percentile function using "ecdf" [Empirical Cumulative Distribution Function]
# using a data file "percentiledata" with variable VALUE
percentileFUN <- ecdf(percentiledata$VALUE)
percentileFUN
percentileFUN(percentiledata$VALUE)
# write the percentile values to the source file
percentiledata$pctl <- percentilefunction(percentiledata$VALUE)
#
View gist:7740998

Random numbers in R

The creation of random numbers, or the random selection of elements in a set (or population), is an important part of statistics and data science. From simulating coin tosses to selecting potential respondents for a survey, we have a heavy reliance on random number generation.

R offers us a variety of solutions for random number generation; here's a quick overview of some of the options.

runif, rbinom, rnorm

One simple solution is to use the runif function, which generates a stated number of values between two end points (but not the end points themselves!) The function uses the continuous uniform distribution, meaning that every value between the two end points has an equal probability of being sampled.