User Tools

Site Tools


r-tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
r-tutorial [2011/10/12 23:57]
liuyipei
r-tutorial [2011/10/13 14:22]
liuyipei
Line 12: Line 12:
 # R as a graphics tool # R as a graphics tool
 # Define a vector named cars with five values # Define a vector named cars with five values
-cars <- c(1,3,6,4,9)+my.vector <- c(1,3,6,4,9)
  
-Plot the values in cars with default options +Some basic variables that come with R to play with 
-plot(cars)+head(cars) 
 +dim(cars)
  
-r <- rlnorm(1000) +plot(x=cars$speedy=cars$dist
-h <- hist(r, plot=F, breaks=c(seq(0, max(r)+1, by=0.1))) +     main="cars", 
-plot(h$counts, +     xlab="speed", 
-     log="xy", +     ylab="dist"
-     pch=20, +hist(cars$speed)
-     col="blue"+
-     main="log-normal distribution", +
-     xlab="Value", +
-     ylab="Frequency")+
  
 # Primitive data types # Primitive data types
Line 181: Line 178:
  
 my.data <- as.matrix(read.table("http://www.stanford.edu/~kjung/my.data.txt", sep="\t")) my.data <- as.matrix(read.table("http://www.stanford.edu/~kjung/my.data.txt", sep="\t"))
-                      
  
 # The columns of a matrix can have symbolic names # The columns of a matrix can have symbolic names
Line 280: Line 276:
 load(file="my.image.RData" load(file="my.image.RData"
 ls() ls()
-     
  
-# source - read and execute from a text file containing an R script+</code>
  
-# Debugging - Two main options 
-# Sprinkle cat(...) liberally throughout your code to monitor what is going on. 
-# debug(), undebug() - Turn on/off step through debugging for a function 
  
-# SIMPLE STATISTICS +====Problem Set 1 Hints and Other Tricks==== 
-# Some simple statistics - note that many functions that perform statistical tests +<code> 
-# etc, in R return list objects.   +setwd('/Users/liuyipei/BMI215/Module 1 - Nick/DrugSafety-Homework'
-help(t.test+dz<-read.csv('single_drug_event_frequencies.csv') 
-ttest.result <- t.test(my.data['Hairless',] ~ pheno.data$plays.tuba+head(dz)
-ttest.result +
-names(ttest.result) +
-ttest.result$statistic +
-ttest.result$p.value+
  
-help(cor.test+cd1<-read.csv('cholesterol_drugs.txt', col.names=F
-cor.results <- cor.test(my.data['Hedgehog',]my.data['Shaggy',]method="spearman")+colnames(cd1)<-c('singlet'
 +cd1$chole<-'choles' 
 +head(cd0) 
 + 
 +cd0<-data.frame(singlet=setdiff(as.character(unique(dz$singlet)), cd1),chole=0) 
 +cd0$chole<-'noncholes' 
 +cd.table<-rbind(cd1, cd0) 
 +head(cd1) 
 +head(cd0) 
 +head(cd.table) 
 + 
 +q1.table <- merge(cd.table, dz) 
 +q1.table$hifreq <- ifelse(q1.table$freq > 0.1, 'hifr', 'lofr'
 +head(q1.table) 
 +nrow(q1.table) 
 +ncol(q1.table) 
 + 
 +sum(q1.table$chole == 'choles'
 +sum(q1.table$chole == 'noncholes'
 +table(q1.table$choleq1.table$hifreq) 
 + 
 +library(plyr) 
 +hard.work<-function(x){ 
 +  c(nrow(x), ncol(x), x$chole[1]=='choles'
 +
 +ddply(q1.table.(singlet).fun=hard.work)->lets.talk.about.what.happened 
 +head(lets.talk.about.what.happened) 
 +dim(lets.talk.about.what.happened) 
 +summary(lets.talk.about.what.happened) 
 +colnames(lets.talk.about.what.happened)<-c('singlet', 'row.count', 'col.count', 'ch.drug')
  
-help(chisq.test+library(caTools
-chisq.results <- chisq.test(pheno.data$rides.bike, pheno.data$plays.tuba+my.x<-c(0:10)*0.1 
-chisq.results <- chisq.test(table(pheno.data$rides.bikepheno.data$plays.tuba))+my.y<-c(0,1,3,5,6,7,7,9,9,10,10)*0.1 
 +plot(my.xmy.y) 
 +trapz(my.x, my.y)
  
-help(prcomp+data.frame(a=c(8,12),b=c(31,41))->
-prcomp.results <prcomp(my.data+t 
-my.data[is.na(my.data)] <- 0 +fisher.test(t)->f.t 
-prcomp.results <- prcomp(my.data)+ls(f.t
 +f.t$p.value 
 +f.t$conf.int
  
 </code> </code>
r-tutorial.txt · Last modified: 2011/10/15 12:05 by liuyipei