My R Cheat Sheet
/I was updating my R cheat sheet and thought it'd make a good first post for the blog. I print out this sheet and always keep it within arms length so I can quickly look up code that I frequently use and/or frequently forget. It's an incredibly useful practice.
When I update the cheat sheet (every six months or so), I'll throw in whatever notes I have scribbled and remove code that I think is no longer useful - either because I've memorised it or no longer use it. I change the ggplot segment the most, as it could very easily devolve into an entire cheat sheet on its own (and that's what this page is for).
rm(list = ls())
setwd("C:/Evan/R")
getwd()
files <- list.files("csvs/")
cbpalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") #Colour-blind palette for graphs
var <- read.csv("data.csv", header=TRUE)
var$newcol <- var$oldcol*5
var <- var[-1] #delete first row
new.df <- data.frame(var1,var2,var3)
colnames(new.df) <- c("title1", "title2", "title3")
install.packages("")
install_github('evpickett/evp') #personal package from github
library()
str(x) #identify variable types
class(x) #identify class (e.g. data frame)
as.array(x) as.data.frame(x) as.factor(x) as.numeric(x) as.logical(x) as.character(x)
as.Date(x, "%d/%m/%y", origin="31/12/1870")
# %d (1-31) %a (Mon) %A (Monday) %m (01-12) %b (Jan) %B (January) %y (12) %Y (2012)
as.numeric(as.character(x)) #factor to decimal
methods(as) #for complete list of above
seq(from,to,by=)
rep(c(1,2,3),2) rep(c(1,2,3), each=2) #= 1,2,3,1,2,3 & 1,1,2,2,3,3
url <- sprintf("http://www.fa.ke/%s/%s-%s/%s.htm",City,Month,Year,StationNumber) #Paste together url - %% if % is in string
p1 <- ggplot(data, aes(x=x,y=y,colour=group)) + #group, colour, fill, alpha, linetype
geom_point() +
facet_wrap(~var, ncol=3, scales="free_y") +
guides(colour=FALSE) + #Remove legend (for colour)
scale_fill_discrete(name="Experimental\nCondition", #Legend name (for fill)
breaks=c("ctrl", "trt1", "trt2"), #Legend order
labels=c("Control", "Treatment 1", "Treatment 2"),
values=cbpalette) + #Legend labels
theme_evp() + # Personal graph theme
theme(legend.justification(0,1), legend.position(0,1), legend.box.just="left") #(0,1) = top left; (0,0) = bottom left
ggsave(filname="a.jpg", plot=p1, width= , height= , units="mm", dpi=300) # x4 width/height from console size
##DATA PLAY##
ddply(temps,"factor",function) #Separately run function on temps by factor
ddply(temps, c("factor1", "factor2"),summarise,
N=length(temp),
mean=mean(temp),
sd=sd(temp)) #Summarise data frame by factors
apply(x,margin,function) #margin: 1=rows; 2=columns
by(x[,1:4],factor,function) #run function by factor - RETURNS AS LIST - use ddply for data frame
sapply(x,function) #apply function to each column
replicate(10, rnorm(10)) #replicate function - results to each column
melt(x, id=) #reshape data from columns according to id
cast(x, id ~ var1 + var2, length, value.var = "va1")
tab <- merge(tab, lu.tab, by="lu.value") ##LOOKUP TABLE
rbind(big, add) #merge rows
ifelse(test, yes, no)
substr(x,1,4) #LEFT() 1=first digit, 4=last
test <- function (x) {
return(x) #return list for multiple outputs
}
write.table(df, "sd.csv", sep=",", col.names=NA)
##Libraries to run on startup
/etc/Rprofile.site # (open as admin)
.First <- function() {
library()
}
##Run console in external editor
library(rite)
sinkstart(echo=F)
sinkstop()
##Deploying an app
library(shinyapps)
deployAppMost of this code is very general stuff, so feel free to steal and adapt for your own cheat sheet.