Skip to content

Instantly share code, notes, and snippets.

require(plyr)
require(ggplot2)
require(RColorBrewer)
Df <- data.frame(Method = replicate(100,sample(c("A","B","C", "D", "E"),1)),
Year = sample(c(1980:2014), 100, replace = T))
Df$count <- 1
Df <- Df[order(Df[,2]), ] # Sort by year
@dantalus
dantalus / gist:e181ea23abc6e82239ab
Created January 13, 2015 14:11
Summarize data using R for latex tables
summaryA <- function(data, ...){
# Example
#
# data <- data.frame("A" = rnorm(500, 0, 1),
# "B" = rnorm(500, 0, 1),
# "C" = factor(sample(c("Ca", "Cb"), 500, replace = T)))
#
# a <- summaryA(data)
#
@dantalus
dantalus / gist:ea46eed02eb3b8d6f873
Created January 13, 2015 14:16
LaTeX table rows
Sex & 3 & & & & \\
\multicolumn{1}{r}{Male} & & 51\% & 50\% & 60\% \\
\multicolumn{1}{r}{Female} & & 49\% & 50\% & 40\% \\
Gestational Age (wks) & 0 & 40.3(39.3 to 41) & 40.3(39.3 to 41) & 40.1(39.4 to 41) \\
Birth weight (g) & 3 & 3460(3150 to 3770) & 3430(3120 to 3720) & 4390(4065 to 4605) \\
Birth length (cm) & 71 & 50.2(49 to 51.8) & 50(48.9 to 51.5) & 53(51.5 to 54) \\
Fat mass (\%) & 531 & 10.9(8.2 to 13.8) & 10.6(8 to 13.4) & 16(13.1 to 18.5) \\
Age (yrs) & 0 & 30(28 to 33) & 30(28 to 33) & 31(28 to 33) \\
Height (cm) & 0 & 165(161 to 168) & 165(161 to 168) & 165(160.2 to 169) \\
Weight (kg) & 0 & 65(59.2 to 74) & 65(60 to 74) & 65(58 to 75.8) \\
@dantalus
dantalus / gist:2f571db27ead7ce08aab
Created January 13, 2015 14:22
Basic LaTeX table
\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{array}
\usepackage{float}
\usepackage[cm]{fullpage}
\begin{document}
\begin{tabular}{p{3.0cm}ccccc}
\toprule
library(gmodels)
library(vcd)
library(magrittr)
# This Works
table(factor(sample(c("Aa", "Ab"), 500, replace = T)),
factor(sample(c("Ba", "Bb"), 500, replace = T)),
dnn = c("A", "B")) %>%
{
correlationL <- list()
i <- 1
for (r in seq(.75, .95, .05)){ # Correlations
for (n in seq(10, 1000, 10)){ # Sample sizes
z <- atanh(r) # Fisher's transformation
sez <- 1 / sqrt(n - 3) # SE of transformed variable
lower <- z - (1.96 * sez)
---
title: "Events and missing data report"
author: "Produced by the Clinical Research Facility - Cork"
date: "`r Sys.Date()`"
output: pdf_document
includes:
in_header: \usepackage{color}
keep_tex: true
---
plotData <- function(data, ...){
library(ggplot2)
library(viridis)
library(ggthemes)
library(ggalt)
for (i in seq_along(data)) {
tryCatch({
# Useful libraries ####
library(readxl) # excel
library(plyr) # Tidy data
library(dplyr)
library(tidyr)
library(ggplot2) # Plot data
library(RColorBrewer)
# Returns a correlation coefficient, rounded, with any trailing zeros printed
corr <- function(x, y, ...){
as.numeric(formatC(round(cor(x = x, y = y,
use = "pairwise.complete.obs"), 2),
format = "f", 2))
}
# Generate the matrix of correlation coefficients. In this example, I want to
# correlate a set of variables (A:F) with one other variable (G), rather than