3 views

Uploaded by f1r3br4nd9427

Tips for R

- MPR213_SemTest2_2013memo
- Data Analysis for the Life Sciences
- Homework 1 AMATH 301 UW
- Core.animation.enter.the.Matrix
- 5 into to RV
- resumen mathstudio
- Consolidated Test2 Ans
- lec8
- MATLAB IN FEM.pdf
- LPU MATHEMATICS FOR ECONOMISTS.pdf
- Review of Probability
- Improve Speed in R Loops
- Chapter
- Consideration of a Moving Mass Effect on Dynamic Behaviour of a Jib Crane Structure
- EMW Techniques3_2D
- 46464664
- Electronic Engineering 2014
- Enter Post Title Here
- Model FOC MAnual
- PhD_Dissertation1.pdf

You are on page 1of 2

Launch R

GUI (Windows or RAqua) ESS within XEmacs Terminal To quit, type q(). Double-click icon M-x R (Esc, then x, then R) R

Scatterplot Line plot Add points Add a line Histogram Kernel density plot Contour plot plot(x, y) plot(x, y, type = "l") points(x, y) lines(x, y) hist(x) plot(density(x)) contour(x, y)

Vectors: 1D arrays with undened length. Vector (undened type/length) var <- array() Integer vector var <- 10:20 Numerical vector var <- c(2, 13, 44) Numerical vector var <- seq(5, 10, by = 0.5) Character vector var <- rep("file", 15) Use c() and rep() with numeric, character, and logical values.

To generate a logical vector (TRUE/FALSE), use logical operators compare two vectors of the same length: var1 <- c(1, 3, 5, 7, 9); var2 <- c(1, 2, 3, 4, 5) logical <- var1 == var2 Use logical vectors to recode other vectors or matricies. Putting a logical statement in square brackets extracts only those elements for which the logical statment is TRUE: var3 <- var1[logical] or var3 <- var1[var1 == var2] A factor vector is a special vector that separates each unique value of the vector into either indicator variables (for unordered factors) or a Helmert contrast matrix (for ordered factors) in regression functions.

source("http://gking.harvard.edu/zelig/install.R")

Title X-axis Y-axis X-axis Y-axis Colors label label limits limits main = "My Title" xlab = "Independent Var" ylab = "Dependent Var" xlim = c(0, 10) ylim = c(0, 1) color = "red" or color = c("red", "blue")

Default R prompt Execute a command Comment rest of line Store objects Separate arguments for functions > Press Return or Enter # <,

Call the le to which you will store the plot: For .eps les: ps.options(family = c("Times"), pointsize = 12) postscript(file = "mygraph.eps", horizontal = FALSE, paper = "special", width = 6.25, height = 4) For .pdf les: pdf(file = "mygraph.pdf", width = 6.25, height = 4, family = "Times", pointsize = 12) Draw your plot (it wont display to screen). Close and save the le using dev.off().

Save objects Save workspace Save workspace to le save(x1, x2, file = "object.RData") save.image() save.image(file = "May21.RData")

mat <- matrix(NA, nrow = 5, ncol = 5) mat <- cbind(v1, v2) mat <- rbind(v1, v2) Create an array arr <- array(NA, dim = c(3, 2, 1), dimnames = list(NULL, NULL, "x1")) Extracting or recoding elements in a matrix or array: Row in a matrix mat[5, ] Column in a matrix mat[ , 5] 3rd dimension in a 4D array arr[ , , 5, ] Create a matrix

Common commands

List objects in workspace Remomve objects from workspacs Length of a vector Dimensions of a matrix or array Names for lists or data frames Type of object Summary (for most things) Cross tab Loading packages Quitting R Batch mode ls() remove(x1, x2) length(x) dim(x) names(x) class(x) summary(x) tabular(x) library(PACKAGE) q() source("myfile.R")

Math operations + - \ *

For vectors or arrays of the same dimension, R performs math operations on each (i) or (i, j ) or (i, j, . . . , n) element with its corresponding element in the other vector or array.

Matrix operations

Transpose Inverse Matrix multiplication t(P) solve(P) P %*% Q

dierent types (numeric, logical, factor, and character) at the same time. Lists have a exible number of elements and can be enlarged on the y. Create a list ll <- list(a = 5, b = c("in", "out")) ll <- list(); ll$a <- 5 Extract list elements ll[[6]] ll$a Remove list elements ll[[6]] <- NULL ll$a <- NULL

R stores all objects in the workspace (or RAM). You can store all types of objects at the same time.

Logical operators

Exactly equals == Not equal != Greater than > Greater than or equal >= Less than < Less than or equal <= And & Or | (Note: = is not a logical operator!)

Numeric 1, 3.1416, NA, NaN, -Inf, Inf Logical TRUE, FALSE Character "Alpha", "beta" (Character values are always enclosed in quotes.)

element has the same length or number of observations. Like a list, each element can be of a dierent class. Use list or matrix operations on a data frame. For a data frame data: View the 5th row data[5,] Extract the 7th variable data[[7]] Extract the age variable data$age Insert a new variable data$new <- new.var

Arrays

An array can have one dimension (a vector), two dimensions (a matrix), or n > 2 dimensions. Arrays hold one type of scalar value.

Delimiters

Functions you use Functions you write N-dimensional arrays ( ) { } N = 1: N = 2: N = 3: $ or [[

The data object is an R data.frame, with special properties: Each variable (column) has a name: To view names: names(data) If names are missing or incorrect, assign correct names names(data) <- c("Y", "X") Observations (rows) may have a name: To view names: rownames(data) If names are missing or incorrect, assign correct names rownames(data) <- 1:nrow(data) Display the 5th row: data[5,] Display the variable Y: data$Y Display a summary of the entire data frame: summary(data)

Binomial

CDF PDF Quantiles Random Draws dbinom(x, size, prob) pbinom(q, size, prob) qbinom(p, size, prob) rbinom(n, size, prob) dbeta(x, shape1, shape2) pbeta(q, shape1, shape2) qbeta(p, shape1, shape2) rbeta(n, shape1, shape2) dpois(x, size, prob) ppois(q, size, prob) qpois(p, size, prob) rpois(n, size, prob) dgamma(x, shape, rate = 1, scale = 1/rate) pgamma(q, shape, rate = 1, scale = 1/rate) qgamma(p, shape, rate = 1, scale = 1/rate) rgamma(n, shape, rate = 1, scale = 1/rate) dnorm(x, mean = 0, sd = 1) pnorm(q, mean = 0, sd = 1) qnorm(p, mean = 0, sd = 1) rnorm(n, mean = 0, sd = 1)

Lists

[ ] [ , ] [ , , ] ]]

Beta

CDF PDF Quantiles Random Draws

Loading data

Change directories (using setwd()) to the directory that contains your data les before attempting to read data into R! Space- or tab-delimited data <- read.table("data.tab") Comma-separated values data <- read.csv("data.csv") Stata .dta le library(foreign) data <- read.dta("data.dta") SPSS .sav le library(foreign) data <- read.spss("data.sav", to.data.frame = TRUE)

Poisson

CDF PDF Quantiles Random Draws

Distributions

For all distribtions, let x, q be vectors of quantiles p be a vector of probabilities n be a scalar (the number of random draws).

Gamma

CDF PDF Quantiles Random Draws

First row = variable names: data <- read.table("data.tab", header = TRUE) Missing values = 9 (for example) data <- read.table("data.tab", na.strings = "-9") (Recodes missing values as R NA values.) (You can combine both options.)

Uniform

CDF PDF Quantiles Random Draws dunif(x, min = 0, max = 1) punif(q, min = 0, max = 1) qunif(p, min = 0, max = 1) runif(n, min = 0, max = 1)

Normal

CDF PDF Quantiles Random Draws

Bernoulli

Same as Binomial with size = 1.

- MPR213_SemTest2_2013memoUploaded byNtate Moloi
- Data Analysis for the Life SciencesUploaded bylawyerjj
- Homework 1 AMATH 301 UWUploaded byargo82
- Core.animation.enter.the.MatrixUploaded bytdkhoa
- 5 into to RVUploaded bytamann2004
- resumen mathstudioUploaded byHector Herrera Chavez
- Consolidated Test2 AnsUploaded bySvr Ravi
- lec8Uploaded bySushanta Kumar Roy
- MATLAB IN FEM.pdfUploaded bySingh Ajay
- LPU MATHEMATICS FOR ECONOMISTS.pdfUploaded bySivasankarChinnasamy
- Review of ProbabilityUploaded bychanphat01001
- Improve Speed in R LoopsUploaded byReka Mani
- ChapterUploaded byallinac
- Consideration of a Moving Mass Effect on Dynamic Behaviour of a Jib Crane StructureUploaded byhamadani
- EMW Techniques3_2DUploaded byjustdream
- 46464664Uploaded byCarl Franks
- Electronic Engineering 2014Uploaded byMah
- Enter Post Title HereUploaded bySim Mick
- Model FOC MAnualUploaded byAlleluia Pushpa
- PhD_Dissertation1.pdfUploaded bymoomeejee
- NumPyUploaded byVarun Akuthota
- Least Square Solution-hsmallikUploaded bySagar Rawal
- De Meur y Berg-Schlosser (1994)Uploaded byDafne García
- hernanrobins_v1.10.32.pdfUploaded bymarkkkkkkkheeess
- Flow ChartUploaded byRavindra Sandaruwan Ranaweera
- A primer on R programmingUploaded bynalluri_08
- Week-6(c)Uploaded byprasad9440024661
- How to solve itUploaded byAnonymous C9NZIj
- Class 1 - SlidesUploaded byDebdeep Samajdar
- Hernanrobins v1.10.33Uploaded byRahul Dey

- Wickham - JSS11 - PlyrUploaded byf1r3br4nd9427
- R Statistics GuideUploaded byf1r3br4nd9427
- Plagiarism by Student ProgrammersUploaded byf1r3br4nd9427
- Win BugsUploaded byf1r3br4nd9427
- R_pchUploaded byf1r3br4nd9427
- buttner - JAP 07 - microarrayUploaded byf1r3br4nd9427
- Chelvarajan - Leukocyte Biol 06 - Aging Micro ArrayUploaded byf1r3br4nd9427
- Calculus Cheat SheetUploaded byDino
- Man Ova 2Uploaded byf1r3br4nd9427
- Example Problems Unit I Notes StatsUploaded byf1r3br4nd9427

- multimedia system notesUploaded bySrinath Kv
- FX3U-ENET_UserManual_JY997D18101-F (1)Uploaded byHuy Hóm Hỉnh
- Band in a Box Handout 07Uploaded byFrankDuro
- LAMPARAS DE OBSTRUCCIONUploaded bySergio Polanco
- dual vioUploaded byKarthikeyan Manisekaran
- dryview_8150Uploaded byEduardo Saul Mendoza
- CBCC Amp 2Uploaded bysru_1990
- Review QuestionsUploaded byAndrew George H
- Canon I-sensys Mf5940dn Mf5980dwUploaded by4555556
- OpenMPUploaded bymacngocthan
- Class17 18 IBIS Io Buffer ClassUploaded byzaddler
- Research paper on arduino and its applicationUploaded byKunal
- USBDM_CF_JMxxCLD_V3Uploaded bySergio Johann Comas
- ceph.pdfUploaded bykunalsing
- Linux - DM - 03 - FLM FailbackUploaded bymaheshsekar25
- 1903 9485 95_P-37 IVRUploaded byrsengineers
- Carrier Control 02 2008Uploaded byJavier Sordo
- Manual FordMustang4Uploaded byparadox10001
- Aspire 1700 Series Service GuideUploaded byJim Dandy
- Power Supply Eng91245767806Uploaded bypavlone
- Compute's Programming the Commodore 64 the Definitive GuideUploaded byCubemanPDX
- BrewManiacExManual 0.2.1Uploaded bygon_yo153580
- 01-Huawei DNBS NodeB Commission.pdfUploaded byTeo Pisco
- Keyboard Shortcuts KeyUploaded byfaithreign
- JAVAUploaded bycrazytapan07
- Diagrama electrico WTECIII - Serie 3000 y 4000ALLISON.pdfUploaded byjuan6469
- HomeWork4-สมบ_รณ_Uploaded byCHAINARONG ARUNCHAIWAT
- Lenguage V+ User GuideUploaded byFabian Blanco Piña
- Using the OracleAS 10Uploaded bydbwalya
- CSEC Information Technology January 2010 P1Uploaded byJennique S.