Skip to content

Instantly share code, notes, and snippets.

# Produces list of user installed packages in R (in a readable format).
ip <- as.data.frame(installed.packages()[,c(1,3:4)])
rownames(ip) <- NULL
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
print(ip, row.names=FALSE)
data life;
set sasdata.life;
run;
proc contents data=life out=contents(keep=name label format) noprint; run;
* Creates a dataset with variable name, variable label and variable format.
library(plyr)
modspec <- function(df) {
summary(lm(sbp ~ age , data = df))
}
dlply(df1, .(gender), modspec)
# Produces a model as specified by modspec for gender (male and female)
# Create a data frame from a grouped summary of a response variable
library(plyr)
df <- ddply(df1, .(age.level, gender), summarise, m.mean = mean(sbp), s.sum = sum(sbp))
# age.level gender m.mean s.sum
# 1 age<40 Female 131.1082 786.6491
# 2 age<40 Male 134.8499 539.3997
# 3 age>=40 Female 142.3465 711.7324
# 4 age>=40 Male 143.8150 1294.3354
# Create a 2x2 table of means:
calcstuff <- function(x){
a <- mean(x)
b <- length(x)
paste(round(a,4), " (n=", b,")", sep = "")
}
with(df1, tapply(sbp, list(gender,age.level), calcstuff))
# age<40 age>=40
# Female "131.1082 (n=6)" "142.3465 (n=5)"
library(reshape2)
# existing data df.tmp like:
# ldl.begin group end.ldl.1 end.ldl.2 end.ldl.3 end.ldl.4
# 1 <=3.4 ctl 0.6923077 0.30769231 0.00000000 0.00000000
# 2 3.4-4.1 ctl 0.2622951 0.49180328 0.21311475 0.03278689
# 3 4.1-4.9 ctl 0.0000000 0.28571429 0.57142857 0.14285714
# convert to long
melt(df.tmp, id.vars=c("ldl.begin", "group"))
# Create scatter with text label on the points
library(ggplot2)
p <- ggplot(data = df2, aes(x = idnum, y = response, label = id))
p <- p + geom_point()
p <- p + geom_text(aes(label=id),hjust=0, vjust=0, size=3)
p <- p + geom_smooth(method = 'loess', se = F)
p <- p + xlab("Fitted FEV1")
p <- p + ylab("Standardised residuals")
p <- p + theme_bw()
p <- p + theme(legend.title=element_blank())
# Density plot in R using ggplot
library(ggplot2)
p <- ggplot(data = df, aes(x = response))
p <- p + geom_histogram(aes(y=..density..), binwidth = 0.3, fill="grey", colour="black")
p <- p + geom_line(stat="density", size = 1)
p <- p + geom_rug()
p <- p + xlab("x")
p <- p + ylab("Probability density")
p <- p + theme_bw()
p <- p + theme(legend.title=element_blank())
# Create a dataframe on the fly in a file.
# Note, when executing you need to finish with a carriage return.
nicotine <- read.table(stdin(),header=TRUE)
day active mp score
20120824 1 3 35.2
20120827 0 5 37.2
20120828 0 3 37.6
20120830 1 3 37.75
20120831 1 2 37.75
20120902 0 2 36.0
ods output SolutionF = fixfx_02 FitStatistics=fitstats_02 LRT=lrt_02 LSMeans=lsm_02 Estimates=estdiffs_02;
proc mixed data=work.tmp method = reml ; * asycov asycorr covtest ic ;
class id group_blindint(ref = 'X') time(ref = '1');
model stepcount = baselinescu group_blindint | time / solution outpm = work.outpm_02 ; *influence(effect=id iter=5);
repeated time / subject=id type = ar(1) R RCORR;
lsmeans group_blindint * time / at means e cl ; * tdiff pdiff;
estimate 'mean Y at t3' baselinescu 4310.844
intercept 1
group_blindint 1 0 0