Skip to content

Instantly share code, notes, and snippets.

View kbroman's full-sized avatar

Karl Broman kbroman

View GitHub Profile
@kbroman
kbroman / csv2map.R
Last active August 29, 2015 14:03
Read genetic map from file and convert to map object to use with sim.cross
## simulate a cross, using genetic map from a CSV file
# read map.csv
maptab <- read.csv("map.csv")
# convert to map object
pos <- maptab[,3]
names(pos) <- maptab[,1]
chr <- maptab[,2]
map <- split(pos, chr)
@kbroman
kbroman / read_csv_test.R
Created June 25, 2014 15:59
read.csv in R ver 3.1.0 doesn't want to round numbers
sapply(read.csv("test1.csv"), is.numeric) # 2nd column is factor
sapply(read.csv("test2.csv"), is.numeric) # both columns numeric
# simulate some data and run anova with ranks + Kruskal-Wallis
k <- sample(1:3, 100, repl=TRUE)
eff <- (1:10)/10
pval <- matrix(nrow=length(eff), ncol=2)
dimnames(pval) <- list(as.character(eff), c("anova", "k-w"))
for(i in seq(along=eff)) {
y <- k*eff[i] + rt(length(k), 3) # residuals ~ t(df=3)
pval[i,1] <- anova(lm(rank(y) ~ factor(k)))[1,5]
# these behave badly
read.csv("ragged.csv")
data.table::fread("ragged.csv")
(max_n_col <- max(sapply(strsplit( scan("ragged.csv", what=character()), ","), length)))
> test()
Loading required package: testthat
Testing aRxiv
Loading aRxiv
arxiv_errors : ...
arxiv_search in batches : ..
cleaning the records : ...................
search range of dates : ....
basic searches : .........
sort_by and sort_order args work : ...1
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<link
href="http://arxiv.org/api/query?search_query%3Dti%3Adeconvolution%20AND%20submittedDate%3A%5B199001010000%20TO%20201409062400%5D%26id_list%3D%26start%3D0%26max_results%3D2"
rel="self" type="application/atom+xml"/>
<title type="html">ArXiv Query: search_query=ti:deconvolution AND
submittedDate:[199001010000 TO
201409062400]&amp;id_list=&amp;start=0&amp;max_results=2</title>
<id>http://arxiv.org/api/PmrTD7mnJhK1VVvONMZIkrVVdAg</id>
<updated>2014-09-09T00:00:00-04:00</updated>
A regular code chunk:
```{r regular}
data(cars)
summary(cars)
```
A verbatim code chunk:
options(show.signif.stars=FALSE, scipen=10, htmlhelp=TRUE)
options(width=115, repos="http://cran.rstudio.com",
CRAN = "http://cran.rstudio.com",
browserNLdisabled = TRUE,
deparse.max.lines = 2)
if (interactive()) {
suppressMessages(require(devtools))
}
options(error= function() cat(" \\\ \n \\\
# convert c("female", "Male") to c(0,1)
# magrittr is way nicer than nested function calls
convert_sexcodes <-
function(codes)
{
require(magrittr)
tolower(codes) %>%
# if laundry contains n_pair pairs of socks and n_sing singletons,
# how many socks to draw to get first pair?
sim_socks <-
function(n_pair=21, n_sing=3, n_sim=100000)
{
socks <- c(rep(1:n_pair, 2), n_pair+(1:n_sing))
# replicate(n_sim, min(which(duplicated(sample(socks)))))
library(magrittr)