Presentation is loading. Please wait.

Presentation is loading. Please wait.

R tutorial http://people.musc.edu/~elg26/teaching/methods2.2009/R-intro.pdf.

Similar presentations


Presentation on theme: "R tutorial http://people.musc.edu/~elg26/teaching/methods2.2009/R-intro.pdf."— Presentation transcript:

1 R tutorial

2 R interface Unix, linux, windows batching file: File -> open script
run commands: Ctrl-R Save session: sink([filename])….sink() Quit session: q()

3 General Syntax result <- function(object(s), options…)
Object-oriented programming Note that ‘result’ is an object

4 First things first: help([function]) help.search(“linear model”)
help.start()

5 Choosing your default setwd(“[pathname for directory]”)
need “\\” instead of “\” when giving paths .Rdata .Rhistory

6 Start with data read.table read.csv scan dget

7 Extracting variables from data
Use $: data$AGE note it is case-sensitive! attach([data]) and detach([data])

8 Descriptive statistics
summary mean, median var quantile range, max, min

9 Missing values sometimes cause ‘error’ message na.rm=T
na.option=na.omit

10 Objects data.frame, as.data.frame, is.data.frame
names([data]) row.names([data]) matrix, as.matrix, is.matrix dimnames([data]) factor, as.factor, is.factor levels([factor]) arrays lists functions vectors scalars

11 Creating and manipulating
combine: c cbind: combine as columns rbind: combine as rows list: make a list rep(x,n): repeat x n times seq(a,b,i): create a sequence between a and b in increments of i seq(a,b, length=k): create a sequence between a and b with length k with equally spaced increments

12 ifelse ifelse(condition, true, false) cut(x, breaks)
agelt50 <- ifelse(data$AGE<50,1,0) note for equality must use “==“ cut(x, breaks) agegrp <- cut(data$AGE, breaks=c(0,50,60,130)) agegrp <- cut(data$AGE, breaks=c(0,50,60,130), labels=c(0,1,2)) labels=F)

13 Looking at objects dim length sort

14 Subsetting Use [ ] Vectors Matrices & Dataframes
data$AGE[data$REGION==1] data$AGE[data$LOS<10] Matrices & Dataframes data[data$AGE<50, ] data[ , 2:5] data[data$AGE<50, 2:5]

15 Some math abs(x) sqrt(x) x^k log(x) (natural log, by default)
choose(n,k)

16 Matrix Manipulation Matrix multiplication: A%*%B transpose: t(X)
diag(X)

17 Table table(x,y) tabulate(x)

18 Statistical Tests and CI’s
t.test fisher.test and binom.exact wilcox.test

19 Plots hist boxplot plot pch xlab, ylab xlim, ylim xaxt, yaxt axis

20 Plot Layout par(mfrow=c(2,1)) par(mfrow=c(1,1)) par(mfcol=c(2,2))
help(par)

21 Probability Distributions
Normal: rnorm(N,m,s): generate random normal data dnorm(x,m,s): density at x for normal with mean m, std dev s qnorm(p,m,s): quantile associated with cumulative probability of p for normal with mean m, std dev s pnorm(q,m,s): cumulative probability at quantile q for normal with mean m, std dev s Binomial rbinom etc.

22 Libraries Additional packages that can be loaded Example: epitools
library library(help=[libname])

23 Keeping things tidy ls() and objects() rm() rm(list=ls())

24 Future Topics linear regression sourcing R code creating functions
organizing R files


Download ppt "R tutorial http://people.musc.edu/~elg26/teaching/methods2.2009/R-intro.pdf."

Similar presentations


Ads by Google