A Short List of The Most Useful R Commands
A Short List of The Most Useful R Commands
A Short List of The Most Useful R Commands
A summary of the most important commands with minimal examples. See the relevant part of
the guide for better examples. For all of these commands, using the help(function) or ? function
is the most useful source of information. Unfortunately, knowing what to ask for help about is
the hardest problem.
See the R-reference card by Tom Short for a much more complete list.
Input and display
#read files with labels in first row
read.table(filename,header=TRUE)
read.table(filename,header=TRUE,sep=',')
x=c(1,2,4,8,16 )
y=c(1:10)
n=10
x1=c(rnorm(n))
y1=c(runif(n))+n
z=rbinom(n,size,prob)
vect=c(x,y)
mat=cbind(x,y)
mat[4,2]
mat[3,]
mat[,2]
subset(dataset,logical)
subset(data.df,select=variables,logical)
data.df[data.df=logical]
x[order(x$B),]
x[rev(order(x$B)),]
browse.workspace
moving around
ls()
rm(x)
rm(list=ls())
attach(mat)
new=old[,-n]
new=old[n,]
new=subset(old,logical)
distributions
beta(a, b)
gamma(x)
choose(n, k)
factorial(x)
dnorm(x, mean=0, sd=1, log = FALSE) #normal distribution
pnorm(q, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean=0, sd=1)
dunif(x, min=0, max=1, log = FALSE) #uniform distribution
punif(q, min=0, max=1, lower.tail = TRUE, log.p = FALSE)
qunif(p, min=0, max=1, lower.tail = TRUE, log.p = FALSE)
runif(n, min=0, max=1)
data manipulation
as.data.frame()
is.data.frame()
x=as.matrix()
scale()
round(x,n)
ceiling(x)
floor(x)
as.integer(x)
transform(data.df,variable names = some operation) #can be part of a set up for a data set
x%in%y
y%in%x
all(x%in%y)
all(x)
any(x)
true?
max()
min()
mean()
median()
sum()
var()
sd()
#standard deviation
mad()
fivenum()
table()
scale(data,scale=T)
cumsum(x)
cumprod(x)
cummax(x)
cummin(x)
rev(x)
cor(x,y,use="pair")
aov.ex2 = aov(DV~IV1*IV21,data=data.ex2)
summary(aov.ex1)
print(model.tables(aov.ex1,"means"),digits=3)
boxplot(DV~IV,data=data.ex1)
lm(x~y,data=dataset)
t.test(x,g)
pairwise.t.test(x,g)
power.anova.test(groups = NULL, n = NULL, between.var = NULL,
within.var = NULL, sig.level = 0.05, power = NULL)
power.t.test(n = NULL, delta = NULL, sd = 1, sig.level = 0.05,
power = NULL, type = c("two.sample", "one.sample", "paired"),
alternative = c("two.sided", "one.sided"),strict = FALSE)
lm(Y~X)
lm(Y~X1+X2)
lm(Y~X|W)
solve(A,B)
solve(A)
#inverse of A
factanal()
princomp()
apply(x,1,min)
apply(x,2,max)
col.max(x)
column
Graphics
par(mfrow=c(nrow,mcol))
par(ask=TRUE)
par(omi=c(0,0,1,0) )
hist()
#histogram
plot()
plot(x,y,xlim=range(-1,1),ylim=range(-1,1),main=title)
par(mfrow=c(1,1))
symb=c(19,25,3,23)
colors=c("black","red","green","blue")
charact=c("S","T","N","H")
plot(PA,NAF,pch=symb[group],col=colors[group],bg=colors[condit],cex=1.5,main="P
ostive vs. Negative Affect by Film condition")
points(mPA,mNA,pch=symb[condit],cex=4.5,col=colors[condit],bg=colors[condit])
curve()
abline(a,b)
abline(a, b, untf = FALSE, ...)
abline(h=, untf = FALSE, ...)
abline(v=, untf = FALSE, ...)
abline(coef=, untf = FALSE, ...)
abline(reg=, untf = FALSE, ...)
identify()
plot(eatar,eanta,xlim=range(-1,1),ylim=range(-1,1),main=title)
identify(eatar,eanta,labels=labels(energysR[,1]) )
on the plots
locate()
legend()
pairs()
pairs.panels ()
diagonal
#not standard R, but uses a function found in useful.r
matplot ()
biplot ())
plot(table(x))
x= recordPlot()
replayPlot(x)
#replot object x
dev.control
pdf(height=6, width=6)
dev.of()
layout(mat)
graphic location
layout(rbind(c(1, 1, 2, 2, 3, 3),c(0, 4, 4, 5, 5, 0)))
for (i in 1:5) {
plot(i, type="n")
text(1, i, paste("Plot", i), cex=4)
}
Distributions
To generate random samples from a variety of distributions
runif(n,lower,upper)
rnorm(n,mean,sd)
rbinom(n,size,p)
sample(x, size, replace = FALSE, prob = NULL)
for time
#see ?formats and ?POSIXlt
as.Date
month= months(date)
Additional functions that I have created because I needed some specific operation may be
included in the workspace by issuing the source command:
source(http://personality-project.org/r/useful.r)
#describe
#summ.stats
#error.crosses
#skew
find skew
#panel.cor
#pairs.panels
matrix
#multi.hist
#correct.cor
#fisherz
#paired.r
#count.pairwise
#factor.residuals
#factor.rotate
#phi2poly