R-Cheat-Sheet PDF

Title R-Cheat-Sheet
Course Principles of Psychology
Institution Queen's University
Pages 4
File Size 112 KB
File Type PDF
Total Downloads 106
Total Views 151

Summary

STAT...


Description

R Cheat Sheet Set Up setwd("/Users/racha/Desktop/Intro Stats") getwd() MyData=read.csv("ChickWeight.csv") Vectors Before2000=c(13,1,1,1,1,1) Maximum a=max(Before2000) Mean d=mean(Before2000) To select certain data within dataset: mean=mean(D3$Weight) Standard Deviation sd(SubMyData3$Fertility) Median median(MyData$MetabolicRate) Variance var(MyData$MetabolicRate) Minimum min(MyData$MetabolicRate) Quartiles First Quartile quantile(MyData$MetabolicRate, 0.25) Retrieve 3 quartiles quantile(MyData$MetabolicRate, c(0.25,0.5,0.75)) #3 quartiles Descriptive Stats, including quartiles, IQR, sd

summary(MyData$MetabolicRate) Interquartile Range (3rd quartile – 1st Quartile) contains 50% of data IQR(MyData$MetabolicRate)#interquartile range See Type of Variables in Data str(MyData) Subset Data D3=subset(MyData, Diet=='3') Multiple variables subset SubMyData1=subset(MyData, Year=='1995-2000' & Subgroup=='15-19 yr') See Categories names(MyData) Change Type of Variable in order appeared when names(MyData) applied MyData=read.csv("metabolic.csv",colClasses=c('factor','numeric', 'factor','numeric','factor','numeric')) Contingency Tables Two way table(accepted,department) Three way table(accepted,department,sex) Plot – x variable, y variable t=D1$Time w=D1$Weight plot(x,y) plot(t,w,xlab="Time (Days)", ylab="Weight (Grams)") Boxplot x=MyData$ExerciseLevel > y=MyData$MetabolicRate

boxplot(y~x,col=c(“orange”,”blue”,”green”),ylab=”Metabolic Rate (calories per day)”, xlab=”Exercise Level”)

boxplot(mercury~lake,main="Mercury Concentrations by Lake",ylab='Mercury Concentration (ppb)',xlab='Lake',col=c('lightblue','red'),data=MyData) set y variable against x Change order of data levels(MyData$lake) set to order desired MyData$lake=factor(MyData$lake,c("O","L")) Histogram hist(Lucky$mercury,main="Lucky Lake",xlab="Hg Concentration (ppb)",ylab="Observed Frequency") Barplot barplot(Lucky$mercury,ylim=c(0,100),col='red',main='Lucky Lake',xlab='Sediment Sample', ylab='Mercury Concentration(ppb)') Plot two data sets on one bar graph Select data wanted BothLakes=data.frame(Lucky$mercury,Oddball$mercury) Create matrix of mixed data BothLakes=as.matrix(BothLakes) BothLakesTransform=t(BothLakes) Plot data stacked barplot(BothLakesTransform,xlab="Depth Interval",ylab='Concentration of Mercury (ppb)', ylim=c(0,250), col=c('red','lightblue')) legend legend(15,250,c('OddBall','Lucky'),col=c('red','lightblue'),pch=19) (15,250,c('OddBall','Lucky'),col=c('red','lightblue'),pch=19) plot data beside barplot(BothLakesTransform,xlab="Depth Interval",ylab='Concentration of Mercury (ppb)', ylim=c(0,250), col=c('red','lightblue'),beside=TRUE) Add Line Vertical line, in this case median was calculated of x axis variable and line was indicated on graph abline(v=84.1155,col= 'blue')

Horizontal line abline(h=69.0015,col= 'red')

Distributions pnorm(x,m,sd) x is the upper limit, m is the mean, sd is the standard deviation gives percentage in this range qnorm(p,m,sd) percentage, mean, standard deviation gives range where percentage is found

head(MyData) names(MyData) str(MyData)...


Similar Free PDFs