0% found this document useful (0 votes)
8 views5 pages

Essential R Commands Guide

This document provides a summary of common commands in R for data manipulation, plotting, and basic statistical analysis. It lists R commands for importing and exporting data, subsetting and transforming datasets, creating basic plots and adding elements to plots, fitting statistical models to time series data, and conducting statistical tests. The document is intended as a quick reference guide for common R functions but notes that most commands have additional options not shown.

Uploaded by

Sean Riley
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Essential R Commands Guide

This document provides a summary of common commands in R for data manipulation, plotting, and basic statistical analysis. It lists R commands for importing and exporting data, subsetting and transforming datasets, creating basic plots and adding elements to plots, fitting statistical models to time series data, and conducting statistical tests. The document is intended as a quick reference guide for common R functions but notes that most commands have additional options not shown.

Uploaded by

Sean Riley
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Handy R Stuff by T.R.

Willemain Revised 14 Dec 09

R R CMD BATCH foo.R & q() [Link]() help(plot) library() library(cluster) library(help=cluster) getwd() setwd("foo") source("foo.R") attach(x) detach(x) ls() rm(x,i,xmax) rm(list=ls())

[to start R] [to run R program foo.R as a background job] [to quit] [with browser open, get HTML help] [get help for plot command] [to list available libraries] [to load cluster library] [for help on cluster] [show current working directory] [set current working directory to foo] [run stored R program foo.R] [allows use of variable labels from data frame x] [remove data frame x from search path] [list all objects] [remove objects x, i and xmax] [remove all objects and frame] [read space-delimited data file "[Link]"]

x=[Link]("[Link]",sep=" ",header=FALSE) load("[Link]");attach(x) save(x,file="[Link]") y=cbind(x1,x2,x3) x12=merge(x1,x2,all=T) x=edit([Link]()) edit(x) x=numeric(100) x=rep(0,n)

[load in a pre-stored object x, access variable labels] [save an object for fast retrieval] [create a matrix from individual variables x1,x2,x3] [merge 2 data frames with same columns] [input data for x via interactive grid] [opens a spreadsheet-like grid display of x] [initialize a vector] [initialize a vector of length n with 0's]

x=matrix(NA, nrow=m, ncol=n) [initialize an mxn matrix with NA's]

Note: No guarantees against typo's. Most commands have options not shown here.

x=array(0,dim=c(2,3,4)) x[order(x[3]), ]

[initialize a 2x3x4 array with 0's] [sorts rows of array x on its 3rd variable] [sample 10 values of x] lists indices of X's<0.1] [shows all levels of a factor]

y=sample(x,10,replace=TRUE,prob=NULL) x=runif(100);indices=which(x<0.1) levels(factor)

browser() [Link](5) {Ctrl+z} fg [Link]()

[interrupts execution to go to R prompt; CR resumes] pauses execution for 5 seconds] [key combo to pause execution of R code] [keyboard inputs to resume execution of R code] [time, used twice to compute time intervals]

for(i in 1:100) { x=rnorm(1000,0,1) y=rnorm(1000,1,2) plot(x,y) } if(runif(1)<0.5) x=0 else x=1 if((x==0)&(y > -3)) z=0

[for loop to plot 100 scatterplots]

[if statement] [more complex if statement]

run=1 repeat {

repeat loop and if statement] if(runif(1)<0.5) break the else run=run+1

} print{run}

par(mfrow=c(2,3)) par(ask=TRUE)

[set graph window to show 2 rows and 3 columns of plots] [keep graph on screen until press Enter]

Note: No guarantees against typo's. Most commands have options not shown here.

plot(x,y,main="Title",xlim=c(-1,1),ylim=c(0,1)) text(x,y,labels,pos=1)

[scatterplot w/title,axis limits]

[put labels in vector "labels" on scatterplot]

plot(jitter(count),y) abline(h=0);abline(v=0) [Link](x,y) pairs(x) [Link](x,y) boxplot(y~x) hist(x) qqnorm(x);qqline(x) qqplot(x,y)

[jitters the X vales, for when they are discrete] [adds horizontal and vertical lines at 0 to a plot] [scatterplot with lowess smooth] [scatterplot matrix for all variables in data frame x] [timeplot of 2 series, x and y] [boxplots of y for each level of factor x] [histogram of x] [normality plot with fitted straight line] [quantile-quantile plot for 2 variables]

for (i in 1:4) { windows();plot(rnorm(100,0,1) } [multiple graph windows] library(lattice);splom(~x[,3:7]) library(lattice);coplot(y~x l z) [scatterplot matrix for V3-V7] [separate plots of y vs x for ranges or levels of z] library(lattice);histogram(~x | y) [histogram matrix of x given categorical y]

mean(x,[Link]=TRUE) sd(x,[Link]=TRUE) median(x) [Link](x,y) [Link](x,"punif",0.1)

[mean computed only on non-missing values] [ditto for std dev] [median] [K-S test for 2 datasets] [K-S test against theoretical uniform cdf] [do Kruskall Wallace test, then pull out p-value]

test=[Link](x,y);pval=test[[3]] [Link](x,y) [Link](x,y)

[Student's t test for two groups] [F test for variances]

Note: No guarantees against typo's. Most commands have options not shown here.

tapply(z,list(x,y),mean,[Link]=T) [2-way table of mean of Z vs nominal x and y]

acf(x) pacf(x) y=diff(x,lag=1,differences=1)

[plots of time series x] [plots pacf of time series x] [1st differences; can be generalized]

model=arima(x,order=c(1,1,0)) [fits ARIMA(1,1,0) to time series x] model$coef model$residuals model$aic tsdiag(model) [Link](model$residuals) spectrum(x,spans=c(3,3)) [shows estimated ARIMA coefficients] [the model residuals] [the AIC for the model] [makes 3 plots of residuals from "model"] [plots residuals from fit of "model"] [plots periodogram smoothed by 2 Daniell windows]

f=cut(z,3)

[creates factor f by splitting z into 3 unequal groups] [select subset of frame]

long=subset(data,duration==1,select=v1:v12) ones=unique(data[,1]) filename=paste("foo",".csv",sep="")

[creates a list of unique elements] [creates string "[Link]"]

sink("[Link]") sink() [resumes output to screen]

[redirects all outputs to an output file]

cat(x,y,z,"\n") format(x,digits=3) write("Output of foo.r",file="[Link]") write(date(),file="[Link]",append=T) write("",file="[Link]",append=T)

[prints x,y and z, then line feeds] [prints x with 3 decimal digits] [write header to an output file] [append current time to the file] [append to a blank line to complete header]

write(mean(x),file="[Link]",append=T) [write result to file] [Link](somematrix,outfile,[Link]=F,[Link]=F) [write a matrix] pdf("[Link]");plot(x,y);[Link]() postscript("[Link]"),plot(x,y);[Link]() [creates a pdf file [Link]] [creates postscript file [Link]]

Note: No guarantees against typo's. Most commands have options not shown here.

x[order(x[3]), ]

[sorts array x on its 3rd variable]

Note: No guarantees against typo's. Most commands have options not shown here.

You might also like