R Intro Workshop

27
Intro Workshop Saad Chahine, PhD. May 26, 2014

description

This is an Intro R work shop for graduate students at CSSE

Transcript of R Intro Workshop

Page 1: R Intro Workshop

Intro Workshop

Saad Chahine, PhD. May 26, 2014

Page 2: R Intro Workshop

What is R?“R is a language and environment for statistical computing and graphics… similar to the S language and environment which was developed at Bell Laboratories… by John Chambers and colleagues…”

“R is available as Free Software under the terms of the Free Software Foundation's GNU General Public Licenses in source code form. It compiles and runs on a wide variety of UNIX platforms and similar systems (including FreeBSD and Linux), Windows and MacOS.”

http://www.r-project.org/

Page 3: R Intro Workshop

Getting Started

• Download and launch R

• Type help() press enter

Page 4: R Intro Workshop

Calculator

‘>’ is the prompt line Try: 3+5 Try: 3-5Try: 3/5Try: 3*5Try: sqrt(5)Try: sum (1,3,5)

Page 5: R Intro Workshop

Logic ‘>’ is the prompt line Try: 3<5 Try: 3>5Try: 3+5==8Try: 3+5==9

Page 6: R Intro Workshop

Variables‘<-’ assigns a value Try: x<-24 Try: xTry: x/2Try: xTry: x<-“hello world”Try: xTry: x<-TTry: x

Page 7: R Intro Workshop

Menu Bar

• Saving your documents • Working directories getwd()• Saving workspace

Page 8: R Intro Workshop

Help Example Try: help(ave)Try: help(mean)Try: help(mode)Try: help(median)Try: help(sd)Try: help(t.test)Try: help(anova)

Try: example(ave)Try: example(mean)Try: example(mode)Try: example(median)Try: example(sd)Try: example(t.test)Try: example(anova)

Page 9: R Intro Workshop

Vectors Try: c(3,5,7) Try: c(‘s’,’a’,’a’,’d’)Try: 3:7Try: seq(3,7)Try: seq(3,7,0.25) Try: 7:3Try: name <-c(‘s’,’a’,’a’,’d’)’Try: name [3]Try: name [3] <- ‘d’Try: name [4] <- ‘e’Try: name

Page 10: R Intro Workshop

Vectors Names

Try: ranks <- 1:3Try: names (ranks) <- c(“1st”,”2nd”,”3rd”, )Try: ranks Try: ranks [first]Try: ranks [3] <-4Try: scoRes <- c(450,578,502)Try: barplot(scoRes)Try: names(scoRes) <- c(“Bob”, “Marry”, “Jane”)Try: barplot (1:200)

Page 11: R Intro Workshop

Matrices

Try: matrix(0,6,7)Try: a<-1:42Try: print (a)Try: matrix(a,6,7)Try: seats <- 1:20Try: dim(seats) <- c(2,10) Try: print (seats)

Page 12: R Intro Workshop

Access Values

Try: print(seats)Try: seats[2,3] Try: seats[2,]Try: seats[1,]Try: seats[,3]Try: seats[,5:9]

Page 13: R Intro Workshop

Matrix Try: MATD <-matrix(1:6,2)Try: MATE <-matrix(c(rep(1,3), rep(2,3)), 2, byrow=T)Try: MATE+MATDTry: MATD+10Try: MATD-10Try: MATD+10Try: MATDTry: MATE-MATETry: MATD-MATETry: solve(MATD[,2:3])Try: t(MATE)Try: MATD %*% t(MATE)Try: MATD*100Try: MATD/MATE

Page 14: R Intro Workshop

Factors Try: data = c(1,2,2,3,1,3,2,2,3,3,3,2,2,1,2)Try: fdata=factor(data)Try: fdataTry: mean(data)

Page 15: R Intro Workshop

Import Data CSV1. Find the file path 2. mydata <-read.table(”filepath”,

header=T, sep=“,”)3. mydata <-read.table(”filepath”,

header=T, sep=“\t”)4. For fixed width use read.fwf

Page 16: R Intro Workshop

Import Data SPSS1. Install.packages(“m

emisc”)2. library(“memisc”)3. mydata <-

as.data.set(spss.system.file('/CSSE R Workshop/GEDU6100dataset.sav'))

4. mydata

Page 17: R Intro Workshop

data.frame strstr(mydata)

Page 18: R Intro Workshop

data.frame summarysummary(mydata)

Page 19: R Intro Workshop

data.frame fixfix(mydata)

Page 20: R Intro Workshop

Ways of calling your data- Mean

(mydata$MATH)

- With(mydata, mean(MATH))

- t.test(MATH~GENDER, data=mydata)

Page 21: R Intro Workshop

data.frame attachattach(mydata)mean(MATH)t.test(MATH~GENDER)

Page 22: R Intro Workshop

Export write.table(mydata, “filepath/mydata.txt”)

Note: for export to exl, SAS, SPSS you may need to use foreign package.

Page 23: R Intro Workshop

On your own

Try: data()- Find a data

set and str, summary & one statistical application

Page 24: R Intro Workshop

Useful Packages foreign HmiscprettyR psych ggplot2

summary(mydata describe(mydata)freq(mydata)rcorr(cbind (v1,v2,v3,v4)cor.test (v1,v2, use=“pairwise”)

Page 25: R Intro Workshop

Try this last one… Try: contour(volcano)Try: persp(volcano, expand=0.2)

Page 27: R Intro Workshop

Good Luck!

[email protected]